-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Symbol shorthand syntax as reification step #1
Comments
I think Placing it here may not get the idea as much exposure unfortunately. |
@trusktr make a new repo for your own proposal; it's up to the champions what they want to entertain in a proposal's repo. If the idea turns out to be good, it is up to the champions to incorporate it into their proposal, or, it's up to the committee to advance the new proposal. |
I revisited a little bit my approach, and found out that there are no major reasons to not make it more
To "Proper assignment always has receiver and has no keyword." This leads to following additions (but not replacement, everything shown above is still valid syntax): public #x;
obj.#x = 1; public #x;
existingObj.#y = 1; // throws because #y isn't declared yet
const obj = {
#x: 1,
public #y: 1,
private #z: 1,
};
const otherObj = {
private #x: 1, // shadows `#x` from outer lexical scope
#y: 1, // throws because #y isn't declared yet in this scope
private #z: 1,
}; public #x;
existingObj.#y = 1; // throws because #y isn't declared yet
class SomeClass {
#x = 1;
public #y = 1;
private #z = 1;
};
class SomeOtherClass = {
private #x = 1; // shadowed `#x` not shared with `SomeClass`
public #y = 1; // another `#y` not shared with `SomeClass`
private #z = 1; // another `#z` not shared with `SomeClass`
}; So the only major difference comparing to your existing proposal @jridgewell, is need for the keyword. Does it make sense to you? P.S.I've just got an idea how to add |
So, I've already thought of almost exactly this, but I was limiting it to private #x; // #x is a constant binding.
obj.#x // dot access Except, I was going to allow a obj[#x] // Same as `obj.#x`
// Share the #x reified private symbol
someFunction(#x) // #x is just CallExpression argument,
// providing the reified private symbol This was to support dynamic access usecases. Outer scoped private@bakkot mentioned to me in the last meeting a desire to make all private declarations include the class Ex {
private #x = 1;
} This was specifically to allow sharing an outer scoped private in inner scopes, and to support top-level private eventually: private #x;
private #y;
class Ex {
#x = 1; // Reuses outer #x
private #y; // Declares new #y
} Overall, I like this idea. But, it needs to be discussed in the context of class-fields, not just private symbols. I won't be adding this to the private symbols proposal unless class-fields adopts it first. Adding more changes compared to the class-fields proposal gives the committee more things to complain about, and I'm already fighting an uphill battle. Public keywordI'm a little hesitant to add Provided the committee decides to go with private symbols, I'd be happy to work on this as a follow-on proposal to see what the committee thinks. Protected keywordProtected (as it behaves in other languages) just isn't going to happen in JS. The only way to do it is adding access-guard checks to all property lookups, which the implementers are going to reject outright. But, what most people want with protected is just a shared private symbol. I think this is the path forward, not a |
@jridgewell, you can't even imagine how happy am I to hear that you're working in this direction. If you need any help (docs, readmes, faq, transpilers, tests or whatever else), feel free to contact me (here or directly [email protected] / [email protected]) - I'll be glad to help you.
I didn't mention it explicitly, but my proposal includes
Ok, I understand that. We may exclude it for now.
I didn't even thought that it has to provide exactly same way as in other languages. My idea is related to scope, especially for
But let's keep it aside - it indeed could be follow-up, that we may dicuss later.
Ok, I understand that, but taking @littledan's feedback in tc39/proposal-class-fields#206 (comment) and tc39/proposal-class-fields#183 (comment) into account it seems that there are no proper place to discuss something that relates to both From my side, I'm very interested in
Also, since I don't see how the process works inside, my assumptions about which activities are most valuable could be wrong, so feel free to point the correct direction, which will most likely lead to P.S.I don't know for sure, but I guess that @shannon, @rdking and @hax could be interested in this too. |
Sweet. I think I got confused when you said "proper assignment always has receiver,
I read his comments as "I don't want to work on private symbols", not that he doesn't want to discuss things that affect them both. Changing I know the committee members have privately discussed it, but I don't see it anywhere on the class-fields proposal. There was a strawman proposal to use an class Outside {
#x = 1;
#y = 2;
constructor() {
class Ex {
outer #x = 1; // Reuses outer #x
#y = 3; // Declares new #2
}
}
} I like using
Separate repos please. Or, it can be your own fork of this repo. I just don't want to confuse people who visit this repo, I intend to keep this one as minimal as possible so it's easier to get through committee. |
I'd wager what most people want is both. Familiarity with the meaning of existing keywords helps speed along understanding. So a declaration like |
Originally I posted it in tc39/proposal-class-fields#206, but it seems that
class-fields
were improper place for such proposals.@jridgewell, I've checked your presentation and it seems that following proposal could be used to adjust your
Symbol.private
approach. What do you think about that?Syntax
Declaration
Use
public
,private
(protected
as follow-up) as declaration keyword, likevar
/let
/const
.Assigment
Any assignment without receiver leads to early SyntaxError
Proper assignment always has receiver,
[]
and has no keyword.Declaration + computed property syntax in object literals
The true power comes with computed property syntax.
Declaration + computed property syntax in classes
Work mostly the same way as for objects.
Simple mental model
#
stands forSymbol
. Any variable starting with this sign is ALWAYSSymbol
. So code like thisprivate #x
should be read asprivate symbol x
.Discussible moments
I used already reserved keywords, since we are safe to use them + they are good fit for such mental model. But, obviously, we could select some others, for example:
Possible follow-up proposals
Symbol.protected
/Symbol.friend
/Symbol.<whatever>
and<whatever> #x
declaration syntax;<whatever> #x for 'key'
as shorthand forconst x = Symbol.for('key')
;The text was updated successfully, but these errors were encountered: