What's the status of old-style operator overloads in D2?
Meta via Digitalmars-d
digitalmars-d at puremagic.com
Tue Apr 22 14:35:29 PDT 2014
On Tuesday, 22 April 2014 at 15:50:21 UTC, Daniel Murphy wrote:
> "Andrej Mitrovic" wrote in message
> news:ifghzjafvfqrqkhlpinc at forum.dlang.org...
>
>> Old-style operator overloads (such as opCom, opAnd, etc) have
>> largely been superseded by new-style templated operator
>> overloads (opUnary, opBinary, etc).
>
> I prefer the old ones mainly because the names are better and
> the code easier to read. When you are implementing one
> operator per function (ie most of the time) the extra template
> syntax is just unnecessary noise.
>
> T opMul(T other)
>
> vs
>
> T opBinary(string op : "*")(T other)
>
> The old names were chosen to match what the operations were
> supposed to be, not for the syntax. In that regard the new
> syntax is a step backwards.
Does this work if test is in a different module from main?
struct test
{
private int opBinary(string op: "*")(test other) { return 3; }
public alias opMul = opBinary!"*";
}
void main()
{
test t1 = test();
test t2 = test();
auto n = t1 * t2;
assert(n == 3);
}
More information about the Digitalmars-d
mailing list