operator overloading rewrite rules

Daniel Kozak kozzi11 at gmail.com
Wed Jan 15 10:36:18 PST 2014


On this page http://dlang.org/operatoroverloading.html#Binary, is 
this statement:

"The expression:

a op b
is rewritten as both:

a.opBinary!("$(METACODE op)")(b)
b.opBinaryRight!("$(METACODE op)")(a)"

Which is true when a or b is object or struct, but it doesn't 
work for basic type or arrays wih ufcs.

I mean this:

import std.stdio;

struct S
{
     bool opBinaryRight(string op, T)(T element)
     if (op == "in")
     {
         return true;
     }
}

bool opBinary(string op, T)(T element, T[] array)
if (op == "in")
{
     return true;
}

void main(string[] args)
{
     S s;
     int a;
     int[] b;
     writeln(a.opBinary!"in"(b)); // ok
     writeln(s.opBinaryRight!"in"(a)); // ok
     writeln(a in s); // ok
     writeln(a in b); // doesn't compile
}



More information about the Digitalmars-d mailing list