Ternary if and ~ does not work quite well

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Oct 11 22:23:07 PDT 2015


On 12/10/15 6:19 PM, Andre wrote:
> Hi,
>
> I am not sure, whether the output of following coding is correct:
>
> import std.stdio;
>
> void main()
> {
>      writeln("foo "~ true ? "bar" : "baz");
>      writeln("foo "~ false ? "bar" : "baz");
>      // assert("foo "~ true ? "bar" : "baz" == "foo bar"); does not compile
> }
>
> Output:
>> bar
>> bar
>
> I would expect the output:
>> foo bar
>> foo baz
>
> Also I would expect the assertion to compile.
>
> Kind regards
> André

I read it as:

assert("foo "~ (true ? ("bar") : ("baz" == "foo bar")));

Oh hey look:
/d434/f138.d(6): Error: incompatible types for (("bar") : ("baz" == "foo 
bar")): 'string' and 'bool'

Compiler agrees!


More information about the Digitalmars-d-learn mailing list