What's the status of old-style operator overloads in D2?

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Wed Apr 23 08:42:22 PDT 2014


On Tue, 22 Apr 2014 18:54:41 -0400, Meta <jared771 at gmail.com> wrote:

> On Tuesday, 22 April 2014 at 21:35:31 UTC, Meta wrote:
>> 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);
>> }
>
> And it appears it does.

This changes the dynamics. opMul is not a template, which is important for  
classes.

This also works, and if encapsulated into a mixin, will be a drop-in fix  
for existing opMul and friends code:

struct test
{
    alias opBinary(string op: "*") = blah; // to demonstrate it's not  
actually calling opMul because of old-style operators

    int blah(test other) {return 3;}
}

BTW, I don't know when template alias got so cool, but I like it :)

-Steve


More information about the Digitalmars-d mailing list