-
Notifications
You must be signed in to change notification settings - Fork 5
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
Why not just ?
#3
Comments
it will confuse the parser (? :) |
I don't foresee lots of people writing code like |
what about: |
It's a matter of precedence, just like
I can see how it can be confusing to somebody reading the code, though. |
Above all, it would be very hard to write an efficient parser that distinguishes between: a?[b]+c // interpreted as: (a?[b]) + c
a?[b]+c:d // interpreted as: a ? ([b] + c) : d (And the interpretation of the second expression cannot be changed due to backward compatibility concerns.) |
@bevacqua this shouldn't have been closed that fast.
I wholly agree. It also has been brought up in tc39/proposal-optional-chaining#5:
|
In short, the only reasonable way I have found for a parser to disambiguate the two forms, is to try to parse one way, and, if an error occurs, trash all the work and try to parse the other way. From an implementer perspective, this is unusual and not desirable. Also, from a user perspective, it means that a forgotten or mistyped I am not going to fix this issue on my repo. Formally, that does not mean it won’t be fixed in the official proposal, but personally I strongly doubt it will. |
This works in Swift very well (although I must admit this is really synthetic example):
with rough equivalent in javascript:
In fact Swift doen't allow optional chaining in ternary expressions because Hard to implement sounds like a weak argument. Weird syntax does not benefit anyone. It's possible to throw error/warning and ask developer to add parenthesis or spaces around condition if transpiler cannot disambiguate the expression. Speaking of backward compatibility case:
Basically we are talking about the use of array accessors
What else am I missing here? I see only two ambiguous uses.
Besides the suggested expression ( |
See above. If you are able to propose a solution to that issue that would convince the TC39 technical committee, please discuss it either on es-discuss or on the official repo (https://github.com/tc39/proposal-optional-chaining). |
@claudepache I read discussion, but don't see technical details.
Can you add explanation why do you think so? |
@ikokostya his comment mentions the case:
No human can tell what's going on there and it's impossible to keep backward compatibility if constraints are introduced to disambiguate such cases. |
@pronebird I don't understand you. Provided example
always has deterministic result. What you mean under |
@ikokostya it doesn't. |
It’s also worth pointing out here that JavaScript-parser efficiency is important in the performance of web pages in general, just like it is for the HTML and CSS parsers. JavaScript parsing often takes up a nontrivial amount of time in addition to download and execution. For many devices, this can sometimes add several hundred (or even several thousand) milliseconds on the critical rendering path. For years, TC39 has been careful not to create time-costly syntactic disambiguation/backtracking at every ternary operator in every script. Any such performance regression may significantly increase the first-to-load times (and, of course, monetary losses) of many websites. This is not a luxury that the syntax of an assembly language for the web has, and it is frankly a miracle that TC39 has been able so far to increase its developer ergonomics this much under such tight constraints. When optional chaining is added, its syntax will fulfill those same constraints. Such must be JavaScript’s forward compatibility. |
Hey. I was wondering why not just use
?
instead of?.
.Versus:
I find
obj?.[expr]
to be awkwardThe text was updated successfully, but these errors were encountered: