opCast for classes

"Luís "Luís
Tue Mar 12 13:08:18 PDT 2013


Hi there,

I've been away for a while from the D world, so I guess I missed 
some of the new things.

Regarding opCast, the documentation says:

"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."

Yet this works:

class B
{
     bool v;

     this(bool v)
     {
         this.v = v;
     }

     T opCast(T)()
     {
         return v;
     }
}

unittest
{
     B bfalse = new B(false);
     B btrue = new B(true);
     assert(cast(bool) bfalse == false);
     assert(cast(bool) btrue == true);
}

Is that a new feature? Can I rely on it? Is it documented 
somewhere?

Thanks! :-)

Regards,
Luís


More information about the Digitalmars-d mailing list