Should this Compile?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Oct 3 23:13:00 UTC 2017


On Tuesday, October 03, 2017 22:42:35 SamwiseFilmore via Digitalmars-d-learn 
wrote:
> On Tuesday, 3 October 2017 at 22:37:17 UTC, SamwiseFilmore wrote:
> > Am I using the ternary operator correctly here, or is this an
> > issue with dmd? I'm using dmd v2.076.0.
>
> I wrapped the ternary in parentheses, and it compiled. Still, I'm
> wondering about this behavior.

Operator precedence makes it quite clear how the ternary operator is
supposed to function, and if you're ever not sure, then put parens around
it.

Almost every operator has higher precedence than the ternary operator, so
aside from simple assignments, you typically have to use parens around it.
For some reason, a lot of folks seem to assume that the ternary operator has
much higher precedence than it does and get confused by the result, but if
you just look at the operator precedence table, it should be pretty obvious
why you're seeing what you're seeing:

https://wiki.dlang.org/Operator_precedence

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list