digitalmars.D.learn - implicit conversion of reference to bool?
- John Bell (11/11) Jul 22 2005 I am confused by this in the documentation:
- Stefan (10/21) Jul 23 2005 That's the same as:
- John Bell (7/19) Jul 23 2005 Thanks for the reply. I just thought D worked differently. The
- Manfred Nowak (7/10) Jul 23 2005 [...]
I am confused by this in the documentation: if (cast(B) o) { // o is an instance of B } else { // o is not an instance of B } Does this mean that a reference can be implicitly converted to a bool? Thanks.
Jul 22 2005
That's the same as: where "if (ref)" is a shortcut for "if (ref is null)". It's the same as in C. if() works with any int, where int == 0 evaluates to false and any int <> 0 to true. Hope this helps. Regards, Stefan In article <dbsad1$23q7$1 digitaldaemon.com>, John Bell says...I am confused by this in the documentation: if (cast(B) o) { // o is an instance of B } else { // o is not an instance of B } Does this mean that a reference can be implicitly converted to a bool? Thanks.
Jul 23 2005
Stefan wrote:That's the same as: where "if (ref)" is a shortcut for "if (ref is null)". It's the same as in C. if() works with any int, where int == 0 evaluates to false and any int <> 0 to true. Hope this helps. Regards, StefanThanks for the reply. I just thought D worked differently. The documentation states that the expression checked in an if statement must be a type that can be converted to a boolean. And as I understand it a bool is a bit. And I thought that while a bit could be implicitly converted to an int, an int could not be implicitly converted to a bit. So, the statement had me confused.
Jul 23 2005
John Bell <jdwbell yahoo.com> wrote: [...]The documentation states that the expression checked in an if statement must be a type that can be converted to a boolean.[...] You are right and pointers cannot be converted implicitely to booleans. Therefore the documentation at least misses to describe an exceptional behaviour of the language, that is context dependent. -manfred
Jul 23 2005