d語言之模塊重載this
參考自d程序設計語言---個人博客http://my.oschina.net/u/218155/blog?fromerr=SwOkb7Sw fllow mespa
import std.conv; import std.stdio; //import std.Exception; struct CheckedInt(N)if(isIntegral!N){ private N value; this(N value){ this.value = value; } ref CheckedInt opUnary(string op)()if(op == "++"){ assert(value != value.max); ++value; return this; } ref CheckedInt opUnary(string op)()if(op == "--"){ assert(value != value.min); --value; return this; } CheckedInt opUnary(string op)() if(op =="+" || op == "-" || op == "~") { return CheckedInt(mixin(op~"value")); } //opt cast //string opCast(T)()if(is(T == string)){} //opt +-*/ //CheckedInt opBinary(string op)(CheckedInt){} @property void showValue(){ writeln(value); } } void main() { CheckedInt!int ct = CheckedInt!int(11); ct++; --ct; -ct; ct.showValue; writeln("t"); }