<div dir="ltr">On Mon, Sep 1, 2008 at 9:11 AM, Giles Bathgate <span dir="ltr"><<a href="mailto:gilesbathgate@gmail.com">gilesbathgate@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I am trying to translate the following C# code into D<br>
<br>
public class Test<br>
{<br>
public string Name;<br>
<br>
public static Test operator +(Test lvalue, Test rvalue)<br>
{<br>
if (lvalue == null) { lvalue = new Test(); lvalue.Name = "foo"; }<br>
if (rvalue == null) { rvalue = new Test(); rvalue.Name = "bar"; }<br>
<br>
Console.Write(lvalue.Name);<br>
Console.Write(rvalue.Name);<br>
<br>
return rvalue;<br>
}<br>
<br>
}<br>
<br>
void main()<br>
{<br>
Test T = null;<br>
Test B = null;<br>
T += B;<br>
}<br>
<br>
Whilst leaving the syntax of main() untouched.<br>
<br>
</blockquote></div><br>Long story short: I really don't think you can. Operator overloading in D can only be performed as nonstatic methods of user-defined types. Since a virtual method call requires that the object you're calling it on be non-null (so that you can get the vtable), you can't use overloaded operators on null references.<br>
</div>