Operator overloading, structs
Leandro Lucarella
llucax at gmail.com
Tue Jun 9 08:29:28 PDT 2009
bearophile, el 9 de junio a las 08:19 me escribiste:
> Don:
> > That is, if x is a struct, convert "if(x)" into "if(x==0)"
> > and convert "if(!x)" into "if(x!=0)". Usual rules would then apply, so
> > any opEquals with a parameter which could be implicitly cast from 0
> > would work.
> > That wouldn't allow smart-pointer structs to implement if (x) without
> > requiring them to allow comparisons with integers, so it might be
> > necessary to add a second step: try "if(x==null)" instead.
>
> I don't like this solution, because it relies on a bit of invisible
> magic, and you too have seen it needs extensions because it's not
> general. If you want to use if (x) where x doesn't represent a number
> but something else like a collection, they you have to add even more
> special cases like add a third step: "if(x.length == 0)" etc. So this is
> not a good solution.
I think the more general solution is to allow multiple implicit cast
operators and simply provide implicit conversion to bool for the classes
you want to be able to do if (x) on.
--
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
More information about the Digitalmars-d
mailing list