Ternary if and ~ does not work quite well

Andre via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Oct 11 22:33:55 PDT 2015


On Monday, 12 October 2015 at 05:25:46 UTC, H. S. Teoh wrote:
> On Mon, Oct 12, 2015 at 05:19:38AM +0000, Andre via 
> Digitalmars-d-learn wrote:
>> [...]
> [...]
>
> It's best to parenthesize when mixing other operators with ?, 
> because ? has a pretty low precedence and may "steal" arguments 
> from surrounding operators that you don't intend.  My suspicion 
> is that what you wrote is being parsed as:
>
> 	writeln(("foo " ~ true) ? "bar" : "baz");
>
> which is why you're getting unexpected output. Write instead:
>
> 	writeln("foo " ~ (true ? "bar" : "baz"));
>
> If anything, it also helps readers of your code understand what 
> it does without needing to consult the precedence table.
>
>
> T

Thanks a lot for your answers, it really makes sense.

Kind regards
André


More information about the Digitalmars-d-learn mailing list