November 24, 2013 DMD can implicitly convert class pointer to the bool. Is it bug or terrible feature? | ||||
---|---|---|---|---|
| ||||
Code examle: import std.stdio; class Foo { } void main() { Foo f; if(f) { writeln("f is true"); } else { writeln("f is false"); } f = new Foo(); if(f) { writeln("f is true"); } else { writeln("f is false"); } } Programm output: f is false f is true So, pointer implicitly converts to false if pointer is null and to true if pointer is not null. Is it bug or terrible feature? Note that we have `f is null` syntax for these cases. |
November 24, 2013 Re: DMD can implicitly convert class pointer to the bool. Is it bug or terrible feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ilya-stromberg | On Sunday, 24 November 2013 at 13:49:25 UTC, ilya-stromberg wrote:
>
> So, pointer implicitly converts to false if pointer is null and to true if pointer is not null. Is it bug or terrible feature? Note that we have `f is null` syntax for these cases.
This is neither bug not a terribale feature. Have you coded in C?
|
November 24, 2013 Re: DMD can implicitly convert class pointer to the bool. Is it bug or terrible feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Sunday, 24 November 2013 at 13:57:22 UTC, Maxim Fomin wrote:
> This is neither bug not a terribale feature. Have you coded in C?
Yes, only a little. I like D because it dissallow most of dangerous abbilities. We already have `is` operator for pointer comparison. Class doesn't provide cast to bool. So, why it's allowed?
|
November 24, 2013 Re: DMD can implicitly convert class pointer to the bool. Is it bug or terrible feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ilya-stromberg | On Sunday, 24 November 2013 at 14:02:43 UTC, ilya-stromberg wrote:
> On Sunday, 24 November 2013 at 13:57:22 UTC, Maxim Fomin wrote:
>> This is neither bug not a terribale feature. Have you coded in C?
>
> Yes, only a little. I like D because it dissallow most of dangerous abbilities. We already have `is` operator for pointer comparison. Class doesn't provide cast to bool. So, why it's allowed?
void* ptr;
if(ptr)
was a shortcut for 'if(ptr != NULL)' probably since C was created.
There is no problem with classes or pointers convertion to booleans in condition statements, it is not a dangerous ability. Is operator is not restricted to pointer comparison, you can use it to bitwise compare any objects.
|
November 24, 2013 Re: DMD can implicitly convert class pointer to the bool. Is it bug or terrible feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | Maxim Fomin:
> This is neither bug not a terribale feature.
I think the implicit question of ilya-stromberg was: how much bug-prone is this language feature?
Bye,
bearophile
|
November 24, 2013 Re: DMD can implicitly convert class pointer to the bool. Is it bug or terrible feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ilya-stromberg | > So, pointer implicitly converts to false if pointer is null and to true if pointer is not null. Is it bug or terrible feature? Note that we have `f is null` syntax for these cases.
Not exactly. It is all about "if" condition. AFAIK, D defines that condition `if(X)` get re-written to `if(cast(bool)X)` before semantic pass. So it is kind of implicit explicit conversion :)
|
November 24, 2013 Re: DMD can implicitly convert class pointer to the bool. Is it bug or terrible feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Sunday, 24 November 2013 at 14:12:18 UTC, Maxim Fomin wrote:
> void* ptr;
> if(ptr)
>
> was a shortcut for 'if(ptr != NULL)' probably since C was created.
Small code change:
import std.stdio;
class Foo
{
}
void main()
{
Foo f;
if(f == null)
{
writeln("f is true");
}
if(f != null)
{
writeln("f is false");
}
}
DMD output:
Error: use 'is' instead of '==' when comparing with null
Error: use '!is' instead of '!=' when comparing with null
So, C style 'if(ptr != NULL)' isn't allowed in D.
|
November 24, 2013 Re: DMD can implicitly convert class pointer to the bool. Is it bug or terrible feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Sunday, 24 November 2013 at 14:17:50 UTC, Dicebot wrote:
> Not exactly. It is all about "if" condition. AFAIK, D defines that condition `if(X)` get re-written to `if(cast(bool)X)` before semantic pass. So it is kind of implicit explicit conversion :)
Not exactly.
Code:
bool b = f;
DMD output:
Error: cannot implicitly convert expression (f) of type Foo to bool
But code:
bool b = !f;
compiles.
|
November 24, 2013 Re: DMD can implicitly convert class pointer to the bool. Is it bug or terrible feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ilya-stromberg | On Sunday, 24 November 2013 at 14:18:50 UTC, ilya-stromberg wrote:
> On Sunday, 24 November 2013 at 14:12:18 UTC, Maxim Fomin wrote:
>> void* ptr;
>> if(ptr)
>>
>> was a shortcut for 'if(ptr != NULL)' probably since C was created.
>
> Small code change:
>
> import std.stdio;
>
> class Foo
> {
> }
>
> void main()
> {
> Foo f;
>
> if(f == null)
> {
> writeln("f is true");
> }
>
> if(f != null)
> {
> writeln("f is false");
> }
> }
>
> DMD output:
>
> Error: use 'is' instead of '==' when comparing with null
> Error: use '!is' instead of '!=' when comparing with null
>
> So, C style 'if(ptr != NULL)' isn't allowed in D.
Yes, because D is separate language, and its comparison operator does something special when operands are class references. This is not the same story as in 'if(f)' which is purely bitwise comparison.
I think your question is more appropriate for d.learn.
|
November 24, 2013 Re: DMD can implicitly convert class pointer to the bool. Is it bug or terrible feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Sunday, 24 November 2013 at 14:16:39 UTC, bearophile wrote:
> Maxim Fomin:
>
>> This is neither bug not a terribale feature.
>
> I think the implicit question of ilya-stromberg was: how much bug-prone is this language feature?
>
> Bye,
> bearophile
OK. What are the problems with converting class references to true when they are not null and false when they are null?
|
Copyright © 1999-2021 by the D Language Foundation