-
Notifications
You must be signed in to change notification settings - Fork 113
Alternate strawman idea #94
Comments
To me this is a nonstarter; one of the primary goals is that private means that it can’t be accessed or observed. Otherwise, you can already use symbols for this. Separately, this is a departure from the “scopes” model - you can’t get access to a closed-over variable either, it’s truly private. |
@ljharb I had in a previous version the ability to export fields directly and scope such exports, but I ripped it out as a giant kludge. I would separately consider the more broadly useful concept of scoping exports to specific consumers, though, something like
Conceptually, it's no different than a private instance field with a corresponding getter + setter. So I'd disagree. |
Yes but without the explicit getter/setter, the field - and it’s entire existence - is hidden. That’s the important point. |
@ljharb And that's how my proposal works. I just presented that as another means of explicitly breaking privacy where necessary (one that's a tad less error-prone and verbose enough to be discouraging). |
That's how it works in the current proposal too, by the way. |
See also tc39/proposal-private-fields#93. |
@bakkot Thanks! Updated! 👍 |
Thanks for the reference! |
Edit: Inner classes and shadowed properties already exist in this proposal. Updated to reflect this.
Repo/explainer: https://github.com/isiahmeadows/private-data-proposal
The basic concept for my idea is to use scopes (for a very loose meaning of the term) rather than classes. It's mostly compatible with this proposal on the surface, but it avoids most of the gotchas and doesn't require explicit support for
protected
/friend
/etc. The extensibility was pretty easy to spec, and the only complicating factor was making private fields work with subclasses.Here's some of the benefits it offers:
private
can't be used:And yes, it's fully feature-compatible (and mostly syntactically compatible) with this proposal:
#x = 1
/#x: 1
,#x() {}
, etc., it's implicitly declared if you didn't already declare it outside of the class/object. (This is where 99% of the syntax compatibility comes from.)Notably absent is this: the ability to guard outside access to certain fields to specific classes/scopes/modules. The proper way to handle this is just consenting-adults style: if they say it's private, just consider it private. Unless it's a password or something similarly sensitive (which should be clearly not exported at all), there's no need to truly care too much about it. If it's "protected" or "friend", the subclass or friend class could just choose to expose what you let it see anyways.
If the sigil doesn't suit you, or if the form of declaration itself (the
private #foo, #bar
) seems odd, I'm not too terribly attached to it. The syntax can change - I'm just presenting concepts here to maybe provide ideas.Oh, and pardon the ASI - if it looks like it needs a semicolon, you can probably infer it has one inserted there.
The text was updated successfully, but these errors were encountered: