D1 operator overloads have been deprecated.

Timon Gehr timon.gehr at gmx.ch
Thu Jul 11 19:07:28 UTC 2019


On 11.07.19 19:58, uranuz wrote:
> In change log of nightly builds I see that:
> `D1 operator overloads have been deprecated`
> The major concern about it is that D2 style operators are template 
> functions. And template functions cannot be virtual. Does it mean that 
> we shall not be able to decalare operators in classes and interfaces 
> that could be overloaded in terms of OOP. What is the proposed solution 
> to this problemme?
> 
> Currently the only way I see is to declare `final` opOpAssign(RHS, 
> string op)(RHS rhs) (for instance). And then dispatch them to `regular` 
> virtual functions. But it shall look the same as these operator that we 
> are trying to deprecate, but with extra `wrapper`.


import std.stdio;
class C{
     int x;
     this(int x){ this.x=x; }
     C opAddImpl(C rhs){
         return new C(x+rhs.x);
     }
     alias opBinary(string op:"+")=opAddImpl;
}
void main(){
     auto a=new C(1),b=new C(2);
     writeln((a+b).x);
}

You can probably even write a mixin template that automatically upgrades 
your class from D1 style operators to D2 style.

> What is the profit of deprecating these D1?
> ...

When designing a language from scratch, probably you wouldn't add two 
ways to declare operators, especially if one of them subsumes the other.


More information about the Digitalmars-d mailing list