On Thursday, October 11, 2018 8:35:34 AM MDT James Japherson via Digitalmars-d wrote: > Took me about an hour to track this one down! > > A + (B == 0) ? 0 : C; > > D is evaluating it as > > (A + (B == 0)) ? 0 : C; > > > The whole point of the parenthesis was to associate. > > I usually explicitly associate precisely because of this! > > A + ((B == 0) ? 0 : C); > > In the ternary operator it should treat parenthesis directly to > the left as the argument. > > Of course, I doubt this will get fixed but it should be noted so > other don't step in the same poo.
When parens are used to affect operator precedence, it's _always_ by enclosing the expression. There may be a language out there that does it a different way, but if so, I've never heard of it. Certainly, major languages like C, C++, Java, and C# all do it the way that D does, and they all have the same kind of precedence for the ternary operator that D does. I honestly have no clue how you could ever have gotten the idea that putting parens in front of something could affect operator precedence, since I don't think that I've ever seen anything work that way in any language ever. By putting the parens around (B == 0), you've told the compiler to treat that as a single expression regardless of what operator precendence would otherwise do. You haven't told it to do anything to any other operators. So, A + B == 0 has gone from being equivalent to (A + B) == 0 to A + (B == 0) but that has no effect whatsoever on the ternary operator or any other operator that has lower precedence. So, really, it seems to me that your misunderstanding of operator precedence and parens goes well beyond the ternary operator. Normally, if someone were going to be confused by the operator precedence of the ternary operator, I'd expect them to be confused about the precendence of stuff that happens to the right of the ternary operator when it's in a more complicated expression, not about what happens with parens used in the condition. That stuff is the same as you'd get in any if statement or while loop condition. - Jonathan M Davis