-
Notifications
You must be signed in to change notification settings - Fork 6k
Correct precedence of the null-conditional operators #18037
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
Correct precedence of the null-conditional operators #18037
Conversation
| --------- | ---------------- | | ||
| [x.y](member-access-operators.md#member-access-expression-), [f(x)](member-access-operators.md#invocation-expression-), [a[i]](member-access-operators.md#indexer-operator-), [x++](arithmetic-operators.md#increment-operator-), [x--](arithmetic-operators.md#decrement-operator---), [x!](null-forgiving.md), [new](new-operator.md), [typeof](type-testing-and-cast.md#typeof-operator), [checked](../keywords/checked.md), [unchecked](../keywords/unchecked.md), [default](default.md), [nameof](nameof.md), [delegate](delegate-operator.md), [sizeof](sizeof.md), [stackalloc](stackalloc.md), [x->y](pointer-related-operators.md#pointer-member-access-operator--) | Primary | | ||
| [+x](arithmetic-operators.md#unary-plus-and-minus-operators), [-x](arithmetic-operators.md#unary-plus-and-minus-operators), [\!x](boolean-logical-operators.md#logical-negation-operator-), [~x](bitwise-and-shift-operators.md#bitwise-complement-operator-), [++x](arithmetic-operators.md#increment-operator-), [--x](arithmetic-operators.md#decrement-operator---), [^x](member-access-operators.md#index-from-end-operator-), [x?.y](member-access-operators.md#null-conditional-operators--and-), [x?[y]](member-access-operators.md#null-conditional-operators--and-), [(T)x](type-testing-and-cast.md#cast-expression), [await](await.md), [&x](pointer-related-operators.md#address-of-operator-), [*x](pointer-related-operators.md#pointer-indirection-operator-), [true and false](true-false-operators.md) | Unary | | ||
| `x?.y`, `x?[y]` | [Null-conditional operators](member-access-operators.md#null-conditional-operators--and-) | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BillWagner the more I think, the more I see that the following three solutions might make sense for the precedence table:
- bring
?.
and?[]
back to the first row (pro: it's the same precedence as they are unary post-fix; con: null-conditional operators are not primary (however, we can change the name of the first row from "primary" to "primary and null-conditional") - place
?.
and?[]
into the separate row as the current PR does - remove
?.
and?[]
from the precedence table at all (for example, the spec doesn't mention the precedence of?.
and?[]
explicitly)
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Placing just ?.
and ?[]
in a separate row is not correct. Option 1 is clearly better as it does not introduce more confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BillWagner I've updated the table as per comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for participating in all the conversations on this issue and PR @pkulikov
I'll now.
By this comment the null-conditional operators behave as if their precedence is higher than one of unary operators. I guess that's true because the null-conditional operators are post-fix, and post-fix operators precede pre-fix operators. Still,x?.y
andx?[y]
are not primary expressions. Thus, the separate row for them.Update: partially reverts #18001 (the null-conditional operators changes)