user defined implicit casts

Jarrett Billingsley kb3ctd2 at yahoo.com
Tue Jul 31 19:03:10 PDT 2007


"Reiner Pope" <some at address.com> wrote in message 
news:f8oct2$7le$1 at digitalmars.com...
> I think this situation is very similar to the pseudo-member syntax that 
> many people (including me) are keen on, to generalize the implicit 
> properties for arrays to all types. The same problem arises there, and I 
> would suggest the same solution.

Hm.  I don't really like the idea of just allowing static/global operator 
overloads in isolation, but if they were allowed as another aspect of this 
generalized pseudo-member syntax, it would make much more sense.

> I think the example I gave is one such. Supposing that there is a way to 
> generalise my example to any iteratable list of any numeric type, then I 
> would say that making it a global is the best design. It makes the 
> algorithm composable, independent of the underlying type (like STL). The 
> alternative is to write:
>
> template OpAddImpl(T, ThisType)
> {
>     static if (IsAddable!(T))
>     {
>         ThisType opAdd(ThisType other)
>         {
>             ...
>         }
>     }
> }
>
> class List(T)
> {
>     ...
>     mixin OpAddImpl!(T, typeof(this));
> }
>
> class Vector(T)
> {
>     ...
>     mixin OpAddImpl!(T, typeof(this));
> }
>
> // and the same for all the other list-looking classes

I see what you're getting at now, and I agree that that would be very 
useful. 





More information about the Digitalmars-d mailing list