-
Notifications
You must be signed in to change notification settings - Fork 75
Expected interop with private field access? #28
Comments
I would hope that it would still throw if the slot wasn't present, and only reacted to null/undefined |
Could you clarify what you mean by
I think that is what you're describing too? |
ah, while that's not what I meant, i guess that'd make more sense. |
Yes, class fields as well as private methods will naturally be considered with the obvious semantics. I expect to add a note in the explainer (but it is premature to add that in the spec text proper at this point). |
A possible misconception (or misleading mental model) is to consider |
This has been asked a number of times before. I think the syntax of I agree with others in this thread that I'd prefer the semantics of |
I’ve not thought of a concrete use case, but the way I would try to construct one is the following. Think of a situation where you expect either an object of specific shape (in our specific case, an instance of the current class), or null/undefined. Then, think of an action to trigger with that object (get a value, call a method, etc.), that you want to skip when you have null/undefined. More generally, that situation of “either an object of known shape or null” is (I think) a major source of use cases for optional chaining, be it optional property access, optional method call, optional function call, and even optional property deletion or optional property assignment (#18).
Again, optional chaining has never been intended to “suppress errors”. (At least in my mind.) |
It could also have been interpreted as choose where I let it throw granularly. |
It is difficult to find use cases, because it is unusual to use private fields/methods with anything else than |
@claudepache i don't find it particularly unusual: static isEqual(a, b) {
return a.#id === b.#id; // or something
} also: constructor(stringOrInstance) {
if (typeof stringOrInstance === 'string') {
this.#id = string;
} else {
this.#id = stringOrInstance.#id; // construct via copy
}
} |
Not sure why discussion is starting again now, but I am wondering if we'd be OK omitting this operation for now for the reasons I explained in #28 (comment) . |
I think it doesn't add much value to allow it now - but I also think it makes the language a bit more consistent. Specifically, I'd envision a use case of an initializer of |
I'd like to just be content with the understanding that the syntax composes well, and leave this for consideration to add in the future. This proposal is specifically not trying to add all combinations, but start with the three most useful ones. |
Private element accesses can still occur as part of an optional chain, though, right? class X {
#c;
c() {
return this.a?.b.#c // return this.a != null && this.a.b.#c
}
} |
@adrianheine Good question! I guess this is about whether short-circuiting applies. I'd argue it should apply. |
There’s currently not a grammar production that can handle |
Now that both private fields and optional chaining are at stage 3, the issue of their interaction becomes more pressing. Just to be clear, cases like
|
I’d say both - is there any reason not to? |
Here is my argument why we should support it (both A private field access is (from a user point-of-view) very similar to a (public) property access. Therefore it seems preferable to support both accesses at the same places, whether the access is optional or not. It would be surprising if, for example, As a plausible practical use of that feature, #28 (comment) above suggests the following: static isEqual(a, b) {
// it is assumed that `a`, resp. `b`
// is either an instance of the current class or null.
return a?.#id === b?.#id;
} |
I believe copy constructors and clone methods are valid user cases too. constructor(other) {
this.#prop = other?.#prop;
} static clone(subject) {
const result = new MyClass();
result.#prop = subject?.#prop;
return result;
} Whenever you are dealing with other instances of the same class, this syntax is relevant. |
This adds the `?. PrivateIdentifier` and `OptionalChain . PrivateIdentifier` grammars and related runtime semantics. Closes tc39/proposal-optional-chaining#28
This has been added to the Class Fields proposal. Closed by tc39/proposal-class-fields#301 |
tc39/proposal-class-fields#301 resolved |
Both are added via tc39/proposal-class-fields#301, see tc39/proposal-class-fields#301 (comment). The initial PR added both, with feedback we changed to just |
What is the expected interop with private-field property access? https://tc39.github.io/proposal-class-fields/#sec-updated-syntax
Should
be considered valid syntax?
The text was updated successfully, but these errors were encountered: