d化简属性.

interface A {
	@property int data();
	@property void data(int);
}

alias helper(alias T) = T;

string implement(Interface)() {
	string code;
	import std.traits;
	foreach(memberName; __traits(allMembers, Interface)) {
		string m_name = "m_" ~ memberName;
		// 如果是属性.
		foreach(member; __traits(getOverloads, Interface, memberName))
		static if(functionAttributes!member & FunctionAttribute.property) {
			// 如果不返回空,则为取器
			// 可用返回值造私成员
			static if(!is(ReturnType!member == void)) {
				auto type = (ReturnType!member).stringof;
				code ~= "private " ~ type ~ " " ~ m_name ~ ";\n";

				// 实现取器
				code ~= "@property " ~ type ~ " " ~ memberName ~ "() { return " ~ m_name ~ "; }\n";
			} else {
				// 应为置器,也要实现
				code ~= "@property void " ~ memberName ~ "(typeof("~m_name~") v) { "~m_name~" = v; }\n";
			}
		}
	}

	return code;
}

mixin template A_Impl() {
	mixin(implement!A);
	pragma(msg, implement!A);
}

class B : A {
	mixin A_Impl!();
}

void main() {
	A b = new B();
}
上一篇:代码 bug 嗅探器:Sanitizer


下一篇:java-泛型-参数