digitalmars.D.learn - opCast!bool
- simendsjo (11/11) Jan 03 2012 I guess this is as designed, but I'll ask anyway.
- Jonathan M Davis (7/22) Jan 03 2012 Yeah. It's the same for built-in types. Take arrays and pointers for exa...
- Timon Gehr (3/25) Jan 03 2012 The conversion is explicit. if(x) is rewritten to if(cast(bool)x) and e
- Jonathan M Davis (6/34) Jan 03 2012 It's implicit as far as the programmer is concerned. You do
- Timon Gehr (6/17) Jan 03 2012 There is no 'bool result expected': The relation of the two operands in
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
Jan 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); // falseYeah. 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
Jan 03 2012
On 01/04/2012 12:31 AM, Jonathan M Davis wrote:On Tuesday, January 03, 2012 17:41:12 simendsjo wrote: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.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); // falseYeah. 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
Jan 03 2012
On Wednesday, January 04, 2012 00:35:20 Timon Gehr wrote:On 01/04/2012 12:31 AM, Jonathan M Davis wrote: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 DavisOn Tuesday, January 03, 2012 17:41:12 simendsjo wrote: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.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); // falseYeah. 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
Jan 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); // falseThis tests whether or not typeof(e) implicitly converts to bool, which can be false even if an explicit cast would succeed.
Jan 03 2012