user defined implicit casts

Ender KaShae astrothayne at gmail.com
Sat Jul 28 16:05:15 PDT 2007


Daniel Keep Wrote:

> 
>uggested syntax for overloading implicit casts is:
> > 
> > implicit type1 cast(type2 var);
> > where type1 is the type casting to and type2 is the type casting from
> 
> Eww, ick.  First of all, you're overloading on return type, *and* you've
> introduced a new keyword.  Sorry :)
> 
> D already allows you to define an explicit casting overload like so:
> 
> class Foo
> {
>     type1 opCast() { ... }
> }
> 
> What has been suggested a few times is to make a new operator overload
> that passes the result out using an out argument instead, which you
> *can* overload on:
> 
> class Foo
> {
>     void opImplicitCast(out type1) { ... }
> }

I like that much better than my suggestion, it's a lot cleaner 

> On the issue of using a global function (as opposed to a member
> function), I'd rather get general type extensions, which would also
> provide this as a sort of side effect:
> 
> bool isFoo(Object this)
> {
>     return (cast(Foo)this) !is null;
> }
> 
> auto bar = (new Foo).isFoo;
> 
That would be VERY useful

> 
> That said, I *would* like to see implicit casts added to the language.
> 
> 	-- Daniel




More information about the Digitalmars-d mailing list