www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Feature: Access to lvalue in static operator overloads

reply GilesBathgate <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 05 2007
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Thu, 05 Jul 2007 15:12:49 -0400, GilesBathgate wrote:

 The following C# code is completely impossible in D.

 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
parent Giles Bathgate <gilesbathgate gmail.com> writes:
 ahem ... maybe opAddAssign(a,b) and not oCatAssign(a,b) ?

Jul 06 2007
prev sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
GilesBathgate wrote

 In 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