xxxxxxxxxx
import std.stdio;
struct Foo
{
private int _foo;
this(int i)
{
_foo = i;
}
foo() { return _foo; }
auto opBinary(string op, T)(const T rhs) const
{
static if(is(typeof(rhs) == typeof(this)))
{
//writeln("normative case");
static if(op == "+" || op == "-") return T(mixin("_foo" ~ op ~ "rhs._foo"));
else static assert(0, "Operator " ~ op ~ " not implemented");
}
else
{
//writeln("cool case");
static foreach(m; __traits(getAliasThis, T))
{
static if(is(typeof(__traits(getMember, rhs, m)) == typeof(this)))
{
return T(mixin("this" ~ op ~ "__traits(getMember, rhs, m)"));
}
}
assert(0, "incompatible covering type");
}
}
}
struct Bar
{
Foo f;
alias f this;
int ku;
//alias ku this;
this(Foo f)
{
this.f = f;
}
}
void main(string[] args)
{
auto v1norm = Foo(4) - Foo(3);
pragma(msg, typeof(v1norm));
writefln("v1norm = %s", v1norm);
auto v5 = Bar(Foo(2)) + Bar(Foo(3));
pragma(msg, typeof(v5));
writefln("v5 = %s", v5);
}