-
Notifications
You must be signed in to change notification settings - Fork 73
Why no use .?
as token?
#61
Comments
I've talked with @gisenberg and and @ljharb about this once, and the objection is it's not symmetric between But, I'd still prefer this option over the current proposal ( |
Indeed - the syntax collision with numbers, and the lack of symmetry, are a problem. |
So both
I would too, because of consistency. I understand why |
Another though about this. Currently the optional chaining for functions is |
One option could be |
I don't understand how this proposal is supposed to cover this case then: var a = {1: 'value'};
console.log(a?.1:0); // => will be 0.1 instead of `value` !? Am I missing something? |
@aminpaks you can't do |
So you're saying it has to be |
@aminpaks yes, that's how you'd extract a numeric property with optional chaining. |
I just realized there is different cases, that's why I got confused. I'd highly recommend to include more examples to clarify. var a = {
b: () => 'value-b',
c: ['value-c', undefined, null],
y: null,
};
// optional-chaining
a.b?.(); // => 'value-b'
a.c?.[0]; // => 'value-c'
a.c?.[1]; // => undefined
a.c?.[2]; // => null
a.c?.[3]?.(); // => undefined
a?.y; // => null
a?.z; // => undefined At the end personally think mixing the |
The explainer gives already the three cases: obj?.prop // optional static property access
obj?.[expr] // optional dynamic property access
func?.(...args) // optional function or method call This is easily adaptable to whatever syntax we’ll pick obj.?prop // optional static property access
obj.?[expr] // optional dynamic property access
func.?(...args) // optional function or method call or obj??.prop // optional static property access
obj??[expr] // optional dynamic property access
func??(...args) // optional function or method call or obj?&.prop // optional static property access
obj?&[expr] // optional dynamic property access
func?&(...args) // optional function or method call If something is not clear, please, open a new issue. |
IMO the |
My concern is about readability in the following: obj?.myCallback?.(); // Invokes `myCallback` property if it's not falsy
obj?.myArrayProperty?.[2]; // Returns third element of `myArrayProperty` array if it's not falsy
obj?.myArrayProperty?.[3]?.(); // Invokes forth element of `myArrayProperty` array if it's not falsy The other options are even worth: obj??.myCallback??();
obj??.myArrayProperty??[2];
obj??.myArrayProperty??[3]??(); obj.?myCallback.?();
obj.?myArrayProperty.?[2];
obj.?myArrayProperty.?[3].?(); |
@JoshMcCullough Or you can interpret it this way.
So instead of "get the value of the property |
@tenbits yes, but |
I understand there should be motive, and a reasoning behind the token. But I have to say I did not rationalized this much in the meaning point of view. I do, however, think that we process the token as one, and either its I don't think that when we are programming, we would see this a two characters, but I understand the point. Maybe that's why dislike the |
@JoshMcCullough, again, it depends, I would say, we protect ourself not from actual values neither |
I'd suggest if you were to write one of these
By splitting In that case, why not replace the member accessor entirely? Instead of Or, what if we never care about nullability of any child property?
Lead with a |
@JoshMcCullough there has been many discussion about what you mentioned. Apparently there is some technical difficulties in the ways that prevent the parser to distinguish a ternary
I believe this has been mentioned and discussed too, and as a developer we want to have the power to check the nullability of each level as this can introduce bugs... |
I see it this way:
When you consider the polish notation:
On line btw. As I understand from this proposal, this should be one single operator |
This is it. Is a single token, or operator, compose by two characters, but it will be one, with one single meaning that can't take apart. |
Like == or ++ or += etc. |
Depends how to translate it obj?.anotherOb?.[1];
obj?.someArray?.[0];
obj?.someFunction?.();
// Technically speaking should NOT be this so '?.' counts as one token?
obj?..anotherObj?.[1]; |
@aminpaks I don't really get your point. The current proposal is exactly what you wrote. EDIT: Oh! I see it... The |
Not exactly: obj?..aProperty; // <- two dots
obj?.aProperty; // this proposal
// If we go with a different token then yes you're right
obj??.aProperty;
obj?&.aProperty; |
I suppose if optional chaining is to be extended to work with private fields that would look something like |
I understand that in JS |
|
But the given use case is to access a private member, so the execution is most likely to happen inside the instance of a class, so accessing I mean, in order to have access to a private member, |
@michaeljota no. |
My example was purely to show similarities with private access token, could have just as easily written Currently proposed |
@ljharb Oh! Ok. yes. Good example. Still, I don't think @robbiespeed I get your point now. It's just that is a good use case, in the readme there is no mention about usage with private properties. this.?#b;
this?.#b;
this.#b.?a;
this.#b?.a; I actually like |
Array access would simply be |
@JoshMcCullough That's been discussed many times. They are introducing a new token |
See the FAQ, first item. |
@claudepache / @michaeljota -- Thanks. Although it's ugly IMO, I understand why it's necessary in this case. |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I don't think I have enough experience about tokens in Javascript, but the current token could have some backwards compatibility issues, as far I can tell. In the spec said it at least.
Maybe this is not the reason this haven't go to stage two, but I would like to see this implemented. I understand that
?.
could be confused with a ternary operator using a decimal number as the if clause.But, can
.?
can be use instead? I know that?.
is used in other languages as well, but JS is special, we all know that.Another thing about this, is that you can use properties, and function calls in the same way, so there is nothing unexpected here.
Like I say, I don't know that much about tokens, maybe this was in the table, but I just wanted to say it.
Thanks you.
The text was updated successfully, but these errors were encountered: