Under the Operator associativity heading, there are examples on left associative operators, and right associative operators. None of these examples mention the unary + or the unary -.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@Youssef1313 how do you define associativity for the unary operators?
According to, let's say, Wikipedia:
If an operand is both preceded and followed by operators (for example,
^ 3 ^), and those operators have equal precedence, then the operand may be used as input to two different operations (i.e. the two operations indicated by the two operators). The choice of which operations to apply the operand to, is determined by the associativity of the operators.
I can't think of the example when one operand is both preceded and followed by an unary + or -.
Check C++ Operator Precedence, it mentions associativity for unary operator. I think it's determined by whether the operator comes first or the operand comes first, however, not so sure. So, I opened an issue instead of a PR.
@Youssef1313 thanks for the link. The linked article states the following:
Associativity specification is redundant for unary operators and is only shown for completeness: unary prefix operators always associate right-to-left ... and unary postfix operators always associate left-to-right...
I think this more fits the language specification, rather than "usual" docs, as that appears obvious. I have an impression that everybody interprets the expression -+-+4 as -(+(-(+4))). So, no need to overload the docs with "unary prefix operators are associative right-to-left", which can be confusing instead. Contrary, the docs should mention the cases that doesn't look obvious (like interaction between null-conditional and null-forgiving operators (see the note)).
Sounds good.
@BillWagner, May you move this to the csharplang repo?
Good discussion. This isn't a spec bug. The note @pkulikov has in his previous comment explains why. Consider this fragment:
int x = 6;
int y = -++x; // y is -7. It's evaluated as -(++x)
It's not ambiguous. Both are prefix operators, and bind to the expression to the right of the operator. They must be evaluated in that order: (-++)x doesn't make sense.
I'm not closing this, because I'd like to resolve the question for this page and if an example or a note is needed to explain this.
Thoughts?
@BillWagner as I've mentioned above, I don't see the need to update the page as the behavior looks to be straightforward.
Closing per discussion and upvotes.