opCast!bool

Timon Gehr timon.gehr at gmx.ch
Tue Jan 3 15:40:32 PST 2012


On 01/03/2012 05:41 PM, simendsjo wrote:
> I guess this is as designed, but I'll ask anyway.
>
> http://dlang.org/operatoroverloading.html#Cast says an expression is
> rewritten to opCast "whenever a bool result is expected".
>
> This is true for
> if(e) somethingElse
> and e && somethingElse
>
> , but not for other parts.
> assert(cast(bool)e == true); // explicit cast works
> assert(e == true); // Error: incompatible types for ((s) == (false)):
> 'S' and 'bool'

There is no 'bool result expected': The relation of the two operands in 
== is symmetric. You could just as well say that the result of 'true' is 
expected to be of type typeof(e).


>
> is(typeof(e) : bool); // false

This tests whether or not typeof(e) implicitly converts to bool, which 
can be false even if an explicit cast would succeed.


More information about the Digitalmars-d-learn mailing list