www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - implicit conversion of reference to bool?

reply John Bell <jdwbell yahoo.com> writes:
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
parent reply Stefan <Stefan_member pathlink.com> writes:
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
parent reply John Bell <jdwbell yahoo.com> writes:
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,
 Stefan
Thanks 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
parent Manfred Nowak <svv1999 hotmail.com> writes:
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