import std.stdio; class A { public alias x this; private double x = 1.5; } class B { double opCast() { return x; } private double x = 1.5; } class C { class Proxy { double x = 1.5; } Proxy opDot() { return p; } this() { p = new Proxy; } private Proxy p; } void main() { A a = new A; a = a * 2.0; // alias thisによりaはa.xのように振る舞う writefln(a.x); // 3 writefln(a); // これはmain.Aになる B b = new B; double s = cast(double)b * 2.0; // opCastを定義したことにより明示的にならcast可能になる writefln(s); // 3 C c = new C; double t = c.x * 2.0; // opDotを定義したことによりxを持たないcをスルーしてc.p.xに到達 writefln(t); // 3 }
alias thisを定義したオブジェクトを引数に取るテンプレート関数とか使えそう.