www.digitalmars.com         C & C++   DMDScript  

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

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