Thread overview
opCast!bool
Jan 03, 2012
simendsjo
Jan 03, 2012
Jonathan M Davis
Jan 03, 2012
Timon Gehr
Jan 03, 2012
Jonathan M Davis
Jan 03, 2012
Timon Gehr
January 03, 2012
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'

is(typeof(e) : bool); // false
January 03, 2012
On Tuesday, January 03, 2012 17:41:12 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'
> 
> is(typeof(e) : bool); // false

Yeah. It's the same for built-in types. Take arrays and pointers for example. They don't implicitly convert to bool, but when you use them in a condition, they implicitly convert to bool (true if they're non-null, false if they're null). If you want implicit conversion in general, then you need to use alias this.

- Jonathan M Davis
January 03, 2012
On 01/04/2012 12:31 AM, Jonathan M Davis wrote:
> On Tuesday, January 03, 2012 17:41:12 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'
>>
>> is(typeof(e) : bool); // false
>
> Yeah. It's the same for built-in types. Take arrays and pointers for example.
> They don't implicitly convert to bool, but when you use them in a condition,
> they implicitly convert to bool (true if they're non-null, false if they're
> null). If you want implicit conversion in general, then you need to use alias
> this.
>
> - Jonathan M Davis

The conversion is explicit. if(x) is rewritten to if(cast(bool)x) and e && somethingElse is rewritten to cast(bool)e && cast(bool)somethingElse.
January 03, 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.
January 03, 2012
On Wednesday, January 04, 2012 00:35:20 Timon Gehr wrote:
> On 01/04/2012 12:31 AM, Jonathan M Davis wrote:
> > On Tuesday, January 03, 2012 17:41:12 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'
> >> 
> >> is(typeof(e) : bool); // false
> > 
> > Yeah. It's the same for built-in types. Take arrays and pointers for example. They don't implicitly convert to bool, but when you use them in a condition, they implicitly convert to bool (true if they're non-null, false if they're null). If you want implicit conversion in general, then you need to use alias this.
> > 
> > - Jonathan M Davis
> 
> The conversion is explicit. if(x) is rewritten to if(cast(bool)x) and e
> && somethingElse is rewritten to cast(bool)e && cast(bool)somethingElse.

It's implicit as far as the programmer is concerned. You do

if(x)

without caring that x isn't a bool. That rewrite just explains why it works implicitly in that case but not in general.

- Jonathan M Davis