xxxxxxxxxx
import std.traits;
void something(T, string property)() {
// in THIS context is T a class
// property = "data"
alias S = __traits(getMember, T, property);
// typeof(S) = foo!bar
static if (isCallable!S) {
// some other magic
}
// ...
}
// We need "a class" with a member named "data"
class C
{
// This is going to be `alias S`, so its type must be foo!bar
foo!bar data;
}
// Now we need a type named "bar"
// We know it's going to be used as an argument to `foo`, so let's look there for clues
struct foo(T) {
T get() {
static if (is(T : bar)) {
// `bar` can be compared to null, so it must be a reference type
// That means it's probably a class
if (value is null) {
value = fun!T("id", 123);
}
}
return value;
}
T value;
alias get this;
}
// We don't know anything else about `bar`, so leave it empty for now
class bar { }
// Now let's try instantiating `something`
alias test = something!(C, "data");
TDobj fun(TDobj, T)(string propertyName, T needle) if (is(TDobj : bar)) {
TDobj[] list = loadAllMatched!TDobj(propertyName, needle, 1);
return list.length > 0 ? list[0] : null;
}
// No idea what this is suppose to do, but we can match the signature
TDobj[] loadAllMatched(TDobj, T)(string propertyName, T needle, int i) {
return TDobj[].init;
}