Skip to content

Conversation

pkulikov
Copy link
Contributor

@pkulikov pkulikov commented Apr 23, 2020

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 and x?[y] are not primary expressions. Thus, the separate row for them.

Update: partially reverts #18001 (the null-conditional operators changes)

| --------- | ---------------- |
| [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-) |
Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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

@pkulikov pkulikov changed the title Separate precedence for the null-conditional operators Correct precedence of the null-conditional operators Apr 28, 2020
Copy link
Member

@BillWagner BillWagner left a 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 :shipit: now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants