The problem with the value that is returned from the condition in `static if`. Bug or feature?

sigod via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 6 11:16:27 PDT 2015


On Saturday, 6 June 2015 at 17:06:37 UTC, Dennis Ritchie wrote:
> The problem is that the `static if` it does not work:
>
> immutable hash = [1 : 3, 5 : 7];
>
> static if (5 in hash)
>     writeln("OK");
> // Error: expression &[1:3, 5:7][5]
> // is not constant or does not evaluate to a bool
>
> You have to write something like that :)
>
> immutable hash = [1 : 3, 5 : 7];
>
> static if (!!(5 in hash))
>     writeln("OK"); // prints OK
>
> Pulls whether this issue? Or is it normal?

http://dlang.org/version.html#staticif:

> StaticIfCondition:
>    static if ( AssignExpression )
>
> AssignExpression is implicitly converted to a boolean type, and 
> is evaluated at compile time. The condition is satisfied if it 
> evaluates to true. It is not satisfied if it evaluates to false.

So, I suppose it's should work without casting to bool or `!is` 
operator.


More information about the Digitalmars-d-learn mailing list