DMD can implicitly convert class pointer to the bool. Is it bug or terrible feature?

Maxim Fomin maxim at maxim-fomin.ru
Sun Nov 24 07:19:20 PST 2013


On Sunday, 24 November 2013 at 14:55:10 UTC, ilya-stromberg wrote:
> On Sunday, 24 November 2013 at 14:45:31 UTC, Maxim Fomin wrote:
>> Because '!' operator provides boolean context.
>
> Yes, but it's ambiguously. What should compiler to use:
> 1) pointer comparing `is null` or
> 2) bool comparing `cast(bool)`
>
> What happens if `cast(bool)` operator will be added or removed?

And if instance is null, where compiler can look for opCast? 
Again, why not read the spec and then ask questions?

"Notably absent from the list of overloaded unary operators is 
the ! logical negation operator. More obscurely absent is a unary 
operator to convert to a bool result. ... This only happens, 
however, for instances of structs. Class references are converted 
to bool by checking to see if the class reference is null or not. 
"


More information about the Digitalmars-d mailing list