Operator overloading, structs

bearophile bearophileHUGS at lycos.com
Wed Jun 10 05:23:28 PDT 2009


Don:
> It's very important. If you allow implicit conversion to bool, all kinds 
> of garbage will compile. You might as well abandon static typing.

Some half-backed ideas.
opBool() can be called implicitly when it's required a truth value test, like in if(x), while(x || y), etc.
So this produces a type error:
bool b = x;
and you need the following to call opBool:
bool b = cast(bool)x;
An alternative solution that I think covers most possible usages of opBool is to try replace:
if (x)
with
if(x.length != 0)
(This is what Python does when __nonzero__ method isn't defined).

Bye,
bearophile



More information about the Digitalmars-d mailing list