D1 operator overloads have been deprecated.
Bert
Bert at gmail.com
Fri Jul 12 13:42:35 UTC 2019
On Thursday, 11 July 2019 at 19:07:28 UTC, Timon Gehr wrote:
> 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.
um, the OP said "works with virtual functions/inheritance". When
are people going to learn that aliases are not virtual and have
nothing to do with preserving inheritance structure?
import std.stdio;
class X
{
}
class C : X{
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(){
X a=new C(1),b=new C(2);
writeln((a+b).x);
}
fails.
More information about the Digitalmars-d
mailing list