Feature: Access to lvalue in static operator overloads

Derek Parnell derek at psych.ward
Thu Jul 5 15:26:29 PDT 2007


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



More information about the Digitalmars-d mailing list