Check type at compile-time

This commit is contained in:
Mike Bierlee 2014-12-11 01:40:40 +01:00
parent 4be09dc821
commit 9c749c0cb6

View file

@ -30,16 +30,18 @@ public void autowire(Type)(DependencyContainer container, Type instance) {
foreach (member ; __traits(allMembers, Type)) { foreach (member ; __traits(allMembers, Type)) {
static if(__traits(compiles, __traits( getMember, Type, member )) && __traits(compiles, __traits(getAttributes, __traits(getMember, Type, member )))) { static if(__traits(compiles, __traits( getMember, Type, member )) && __traits(compiles, __traits(getAttributes, __traits(getMember, Type, member )))) {
foreach(attribute; __traits(getAttributes, __traits(getMember, Type, member))) { foreach(attribute; __traits(getAttributes, __traits(getMember, Type, member))) {
if (is(attribute : Autowire) && __traits(getMember, instance, member) is null){ static if (is(attribute : Autowire)) {
alias TypeTuple!(__traits(getMember, instance, member)) memberReference; if (__traits(getMember, instance, member) is null) {
auto autowirableInstance = container.resolve!(typeof(memberReference)); alias TypeTuple!(__traits(getMember, instance, member)) memberReference;
debug { auto autowirableInstance = container.resolve!(typeof(memberReference));
auto autowirableType = typeid(typeof(memberReference[0])); debug {
auto autowireableAddress = &autowirableInstance; auto autowirableType = typeid(typeof(memberReference[0]));
writeln(format("DEBUG: Autowire instance [%s@%s] to [%s@%s].%s", autowirableType, autowireableAddress, memberType, instanceAddress, member)); auto autowireableAddress = &autowirableInstance;
} writeln(format("DEBUG: Autowire instance [%s@%s] to [%s@%s].%s", autowirableType, autowireableAddress, memberType, instanceAddress, member));
}
__traits(getMember, instance, member) = autowirableInstance; __traits(getMember, instance, member) = autowirableInstance;
}
} }
} }
} }