D Logic bug

rikki cattermole rikki at cattermole.co.nz
Thu Oct 11 14:43:47 UTC 2018


On 12/10/2018 3:35 AM, James Japherson 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.

The specification makes this clear (even if another example is needed).

The conditional expression takes an OrOrExpression not an expression as 
the condition. In other words, it requires a boolean operation on the 
left, with Expression's for the branches (deprecated behavior related to 
assignment for else branch but won't go into that).

So not a bug and is working in a reasonable way.

https://dlang.org/spec/expression.html#conditional_expressions


More information about the Digitalmars-d mailing list