digitalmars.D - Feature: Access to lvalue in static operator overloads
- GilesBathgate (37/37) Jul 05 2007 In a previous post I was complaining that I could not access the lvalue ...
- Derek Parnell (38/67) Jul 05 2007 I agree with you, but there is a workaround until your idea becomes
- Giles Bathgate (1/2) Jul 06 2007 ^ Yeah I...
- Manfred Nowak (25/27) Jul 09 2007 In D the lvalue of a static operator overload of a class are all
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
parameters lvalue and rvalue, the lvalue must be of the same type as the
contating type.
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;
}
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 05 2007
On Thu, 05 Jul 2007 15:12:49 -0400, GilesBathgate wrote:...static void Main(string[] args) { Test T = null; Test B = null; T += B; }I agree with you, but there is a workaround until your idea becomes implemented... // -------------- import std.stdio; class Test { string Name; static Test opAddAssign(ref Test lvalue, ref Test rvalue) { if (lvalue is null) { lvalue = new Test(); lvalue.Name = "foo"; } if (rvalue is null) { rvalue = new Test(); rvalue.Name = "bar"; } writefln("%s", lvalue.Name); writefln("%s", rvalue.Name); return rvalue; } } void main() { Test T; Test B; Test.opAddAssign(T,B); } // --------------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);ahem ... maybe opAddAssign(a,b) and not oCatAssign(a,b) ? -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Jul 05 2007
ahem ... maybe opAddAssign(a,b) and not oCatAssign(a,b) ?^ Yeah I keep making misteeks like teh
Jul 06 2007
GilesBathgate wroteIn a previous post I was complaining that I could not access the lvalue in a static operator overload.In D the lvalue of a static operator overload of a class are all those entities of the class that are declared static; one can access them from within the body of the static operator and use that operator in the usual way: import std.stdio; class Test { static char[] Name= "foo";; static void opAddAssign( Test) { writefln("%s", Name); } } void main() { Test T; Test B; T += B; // compiles! } So the issues of your proposal seem to be: 1) Why do one need to initialize instances of classes explicitely? 2) Get rid of the differences between static and non-static! Both would severely disturb some main paradigms of D. -manfred
Jul 09 2007









Giles Bathgate <gilesbathgate gmail.com> 