On 2012-03-28 03:41, Martin Nowak wrote:

Just showing this because it never comes up that one can already implement
runtime reflection using ModuleInfo.xgetMembers and
TypeInfo_Struct.xgetMembers.

module a;
import std.stdio, std.variant;

void main()
{
foreach(m; ModuleInfo)
{
if (m.name != "b") continue;
// getMembers currently doesn't work for classes,
// but it does for modules/structs
auto get = cast(Variant function(string))m.xgetMembers;
auto bfunc = get("bfunc").get!(size_t function());
writeln(bfunc());
}
}


module b;
import std.variant;

// a pointer to this method will end up in ModuleInfo xgetMembers
Variant getMembers(string name)
{
Variant res;
switch (name)
{
case "bfunc": return Variant(&bfunc);
default: return Variant("member "~name~" not found");
}
}

size_t bfunc()
{
return 2;
}

It should be fairly easy to construct some helpers that make the compile
time data digestible.

I had no idea that xgetMembers work at all. But as the comment says, it doesn't work for classes.

--
/Jacob Carlborg

Reply via email to