| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
July 23, 2005 implicit conversion of reference to bool? | ||||
|---|---|---|---|---|
| ||||
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.
| ||||
July 23, 2005 Re: implicit conversion of reference to bool? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to John Bell | That's the same as:
# B ref = cast(B) o;
# if (ref) {
# ...
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.
| |||
July 24, 2005 Re: implicit conversion of reference to bool? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Stefan | Stefan wrote:
> That's the same as:
>
> # B ref = cast(B) o;
> # if (ref) {
> # ...
>
> 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.
| |||
July 24, 2005 Re: implicit conversion of reference to bool? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to John Bell | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply