Anybody have any idea on how to do shared operator overloads?
Ruby The Roobster
rubytheroobster at yandex.com
Thu Jun 2 00:36:23 UTC 2022
A stripped down version of some code I have:
```d
public import std.complex;
public interface Mtype
{
// ...
}
public class Number : Mtype
{
public:
this(Complex!real num = Complex!real(0,0))
{
this.num = num;
}
this(shared Complex!real num = cast(shared
Complex!real)Complex!real(0,0))
{
this.num.re = num.re;
this.im.re = im.re;
}
Number opBinary(string op)(Number rhs) //Standard opBinary
{
mixin("return new Number(this.num " ~ op ~ "
rhs.num);");
}
shared(Number) opBinary(string op)(shared Number rhs)
shared
{
return new shared Number(); //Placeholder untill I
can get the code to work
}
package:
Complex!real num;
}
bool isMtype(T)()
{
bool ret = true;
// ...
shared T p = new shared T();
shared T p2 = new shared T();
ret &= __traits(compiles, T, p + p2);
return ret;
}
static assert(isMtype!Number); //This fails.
```
Upon adding the line shared T c = p + p2 to isMtype, I get the
following error:
source\dutils\math\core.d(138,22): Error: function call through
null class reference `null`
source\dutils\math\core.d(142,15): called from here:
`isMtype()`
source\dutils\math\core.d(142,1): while evaluating:
`static assert(cast(int)isMtype() == 1)`
Anybody know how to get a working shared operator overload and
fix this mess?
More information about the Digitalmars-d-learn
mailing list