Static operator overloads (again)
Giles Bathgate
gilesbathgate at gmail.com
Tue Sep 2 04:44:13 PDT 2008
Giles Bathgate Wrote:
> But static operator overloads are possible in D.
Example:
public class Test
{
public char[] Name;
public static Test opAddAssign(Test value)
{
writefln(value.Name);
//TODO...
}
}
int Main()
{
Test t;
Test b = new Test();
b.Name = "foo"
t += b; // Translates to: Test.opAddAssign(b);
}
The problem with this though is that there is no access to the lvalue t
this is because t+=b; translates to Text.opAddAssign(b); I would like to propose a feature that given the operator overload:
public static Test opAddAssign(Test lvalue, Test rvalue)
{
}
then
t += b;
would translate to:
Test.opAddAssign(t,b);
I really think this would benefit the language, without breaking anything since the compiler can distinguish between single and double parameter static operator overloads.
More information about the Digitalmars-d
mailing list