PROPOSAL: Operator overloading by static member function

oldrev oldrev at gmail.com
Sun Oct 14 00:04:35 PDT 2007


Daniel Keep Wrote:

> 
> 
> oldrev wrote:
> > Daniel Keep Wrote:
> > 
> >>
> >> oldrev wrote:
> >>> It's a simple way to avoid using the ugly "opAdd_r", eg:
> >>>
> >>> struct Currency
> >>> {
> >>>   private int m_value;
> >>>
> >>>   public static Currency opAdd(Currency lhs, Currency rhs) {
> >>>     return Currency(lhs.m_value + rhs.m_value);
> >>>   }
> >>> }
> >> And how are you going to overload static opAdd_r, then?
> >>
> >> I really don't see any need for this; opAdd_r is perfectly fine.
> >>
> >> 	-- Daniel
> > 
> > We do not need opAdd_r anymore, it will be overloaded by static opAdd.
> > 
> > static A opAdd(A a, int i);
> > static A opAdd(int i, A a);
> > 
> > a+100 // it equals to opAdd(a, 100);
> > 100+a //opAdd(100, a)
> > 
> > Regards,
> > - oldrev
> 
> You didn't answer the first question.  You haven't said how we're going
> to do this:
> 
> struct Meters
> {
>     real value;
> }
> 
> struct m
> {
>     static Meters opMul_r(real value)
>     {
>         return Meters(value);
>     }
> }
> 
> auto distance = 20*m;
> 
> In your system, there's no way to have that since you can't have type
> arguments.  And *yes*, there are people who use this kind of thing.
> 
> Also, as Nathan pointed out, this would break basically *all* code that
> overloads any operators and for what?  So we don't have to type "_r" and
> can no longer have static operator overloads?
> 
> 	-- Daniel

Awesome! 
Thanks to point it out! 

Regards,
- oldrev



More information about the Digitalmars-d mailing list