Proposal: user defined attributes
Adam D. Ruppe
destructionator at gmail.com
Wed Mar 21 08:17:20 PDT 2012
On Wednesday, 21 March 2012 at 13:54:29 UTC, Steven Schveighoffer
wrote:
> I think the closest anyone has come is Jacob, with his orange
> library. Maybe he can respond to this point.
I have some runtime reflection in web.d, but the way I do
it is to build up the info at startup, and there's a
special field in the class to hold it.
simplified:
struct Reflection {
string name;
ClassInfo[string] objects;
FunctionInfo[string] functions;
// etc
}
class ApiProvider { immutable(Reflection)* reflection; }
immutable(Reflection)* prepareReflection(T)(T t) {
Reflection* r = new Reflection();
foreach(member; __traits(allMembers, T))
static if(type..)
r.typeInfo[member] = prepareReflection(getMember);
}
To call functions, it makes a wrapper that converts
strings to the right types in a ParameterTypeTuple:
functionInfo.caller = wrap!func;
string delegate(string[][string]) wrap(alias func)() {
return delegate string(string[][string] uriArgs) {
foreach(type; ParameterTypeTuple!func)
blah = to!typeof(blah)(uriArgs[name]);
return to!string(func(args));
}
}
and so on. Of course, the real thing is a lot longer than
this, but you can see the idea.
Once it is all populated at startup, you can do:
myobj.reflection.functions["cool"].call(decodeUriVars("sweet=yes&awesome=def"));
and have it work.
More information about the Digitalmars-d
mailing list