www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - No Access to lvalue in static operator overloads.

reply Giles Bathgate <gilesbathgate gmail.com> writes:
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
parent reply Derek Parnell <derek psych.ward> writes:
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
parent reply Giles Bathgate <gilesbathgate gmail.com> writes:
  (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
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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
parent reply Giles Bathgate <gilesbathgate gmail.com> writes:
 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
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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
parent Giles Bathgate <gilesbathgate gmail.com> writes:
 Well sptted, I he just ment that teh


I am teh l33t!
Jul 05 2007