digitalmars.D - No Access to lvalue in static operator overloads.
- Giles Bathgate <gilesbathgate gmail.com> Jul 03 2007
- Derek Parnell <derek psych.ward> Jul 03 2007
- Giles Bathgate <gilesbathgate gmail.com> Jul 04 2007
- Frits van Bommel <fvbommel REMwOVExCAPSs.nl> Jul 04 2007
- Giles Bathgate <gilesbathgate gmail.com> Jul 04 2007
- Frits van Bommel <fvbommel REMwOVExCAPSs.nl> Jul 04 2007
- Giles Bathgate <gilesbathgate gmail.com> Jul 05 2007
In a previous post I was complaining that I could not access the lvalue in a
static operator overload. I think the reason I was thinking this is because In
C# operator overloads are always static, and all binary overloads take two
parameters lvalue and rvalue, the lvalue must be of the same type as the
contating type.
The following C# code is completely impossible in D.
public class Test
{
public string Name;
public static Test operator +(Test lvalue, Test rvalue)
{
if (lvalue == null) { lvalue = new Test(); lvalue.Name = "foo";
}
if (rvalue == null) { rvalue = new Test(); rvalue.Name = "bar";
}
Console.Write(lvalue.Name);
Console.Write(rvalue.Name);
return rvalue;
}
}
static void Main(string[] args)
{
Test T = null;
Test B = null;
T += B;
}
Now I am not saying this is a bad thing, D is not C# afterall but the current
imeplementation of static operator overloads is not very usefull.
What I prepose is that if the programmer specifies the following code:
public class Test
{
public static Test opAddAssign(Test lvalue, Test rvalue)
{
//...
}
}
When the user writes:
Test a;
Test b;
a += b;
Should compile into:
Test.opCatAssign(a,b);
I have no problem with Non static operator overloads as the lvalue can be
accessed using the this keyword.
Jul 03 2007
On Tue, 03 Jul 2007 15:24:06 -0400, Giles Bathgate wrote:What I prepose is that if the programmer specifies the following code: public class Test { public static Test opAddAssign(Test lvalue, Test rvalue) { //... } } When the user writes: Test a; Test b; a += b; Should compile into: Test.opCatAssign(a,b);
(maybe Test.oAddAssign(a,b); ?) But I like the idea very much. It would solve, in a simple manner, some issues I have. -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Jul 03 2007
(maybe Test.oAddAssign(a,b); ?)
I don't see whay it should be any different from existing operator overloads: http://www.digitalmars.com/d/operatoroverloading.html unless you mean that because its a "special" static overload that it should have a different name to existing static operator overloads? In that case you can tell the difference from the method signature, (so its like an overload overload if you know what I mean)
Jul 04 2007
Giles Bathgate snipped:a += b;Should compile into: Test.opCatAssign(a,b);
Giles Bathgate wrote:(maybe Test.oAddAssign(a,b); ?)
I don't see whay it should be any different from existing operator overloads: http://www.digitalmars.com/d/operatoroverloading.html
I he just meant to say that opAddAssign (as opposed to opCatAssign) would be the correct method for "+=", but then made a typo and missed the 'p'...
Jul 04 2007
I he just meant to say that opAddAssign (as opposed to opCatAssign) would be the correct method for "+=", but then made a typo and missed the 'p'...
Well sptted, I he just ment that teh
Jul 04 2007
Giles Bathgate wrote:I he just meant to say that opAddAssign (as opposed to opCatAssign)
would be the correct method for "+=", but then made a typo and missed the 'p'...
Well sptted, I he just ment that teh
Jul 04 2007
Well sptted, I he just ment that teh
I am teh l33t!
Jul 05 2007








Giles Bathgate <gilesbathgate gmail.com>