-
Notifications
You must be signed in to change notification settings - Fork 23
Precedence #15
Comments
Can you provide some code examples that would behave differently with this change? |
|
What would be the motivation? The current precedence seems sensible to me as it allows for LTR reading; the latter seems like a footgun. |
The motivation would be that |
Mine is mainly it feels weird that x ?? y || z === (x ?? y) || z
x ?? y && z === x ?? (y && z) Both versions of |
Here are what some other languages use for the precedence of Between
Same level as
Higher precedence than comparison operators: |
If we want to preserve expectation (rather than parsing), we must give a || b || c // equivalent to: a ? a : b ? b : c
(a || b) || c // ditto
a || (b || c) // ditto The expectation is: the first of Compare with: a ?? b || c // expection: a != null ? a : b ? b : c
(a ?? b) || c // equivalent to: a != null ? (a ? a : c) : b ? b : c
a ?? (b || c) // equivalent to: a != null ? a : b ? b : c The expectation is: the first of The issue is that “ On the other hand, my experience of that operator in PHP shows that a higher precedence than comparison operators (à la Swift) is probably what is wanted when seeking to minimise the use of parentheses; because I write often something similar to: if (($expression ?? 'default') === 'value') { ... } My conclusion is:
|
Since switching to ?? from || will always be hazardous unless the dev knows the LHS is either truthy or nullish, I'm not sure we'd be minimizing hazards with either approach - which is why i think I lean towards swift semantics. |
I don't really understand this "strictly weaker" idea. If you have a chain of |
I'm curious; what's the current thinking here? Undecided? |
We previously decided on giving |
Thanks! Looks like the summary quote is:
With that being desired behavior (I think I agree fwiw). |
So between "lower" and "same", does that mean that the following holds for
|
Your headings are backwards, but yes. |
Backwards how? |
|
ah yes ty, I’ll fix the table. |
@ljharb Your previous comment is very reasonable for me, but it seems you change your mind? |
I’m not sure if i have changed my mind :-) based on the table, i think I’d prefer the row i marked “same”. |
I think a ?? b || c
a || b ?? c
a ?? b && c
a && b ?? c are all confused though they are a little artificial because I suppose most common use cases like What I think a important case is like But we may meet |
I just need to confirm this is now a SyntaxError as it's not allowed to have ?? w/ || or && blended together in any position. Please let me know if anything changes to update the tests. |
Correct, it’s a syntax error. Parens are required. |
Is there any way of putting an informal note in the spec indicating that the precedence of
and not simply this:
It wasn’t until browsing through the issues that I found out the reason was to postpone the issue of precedence, and to expedite the candidate to Stage 4. Putting a small note in the spec explaining the motivation would make things more clear. There is already precedent (pun intended) for this, in the conditional operator section:
|
There is nothing yet-to-be determined about the precedence; the current spec text is at Stage 4 and on track to be part of ES2020. Following the discussion on this and other theads, it was determined that |
This particular discussion was around For example, if you have
I mentioned this sort of case here: microsoft/TypeScript#39096 and it was resolved as "working as intended." That said, MDN's table does show |
@FFdhorkin Sorry, I've tried to advocate higher the precedence or at least postpone the decision in the meeting of advancing it to stage 4, but I was too late and can't convince the committee (I still think we were too hurry to push it to stage 4, and did not have chance to collect enough feedback from developers on precedence issue). The only thing we can do now is push the tools (IDE/linters/transpilers, etc.) to add checks/rules/warnings to protect the developers. |
The current draft spec gives
??
precedence and parsing in general exactly equal to||
. I wrote the spec this way because I was imagining people upgrading code from||
to??
, and thought that the parsing shouldn't change. However, maybe a lower precedence would make more sense, to be "the lowest expression precedence". Any opinions?The text was updated successfully, but these errors were encountered: