Proposal: user defined attributes
Martin Nowak
dawg at dawgfoto.de
Tue Mar 27 18:41:57 PDT 2012
>>> Note that one library that did attempt runtime reflection capability
>>> (flectioned) does all this at runtime, and does some really funky shit,
>>> like opening /proc/self/map on Linux, or requiring you to pass an
>>> OPTLINK map file. I don't look at these as "innovations" as much as I
>>> do
>>> as workarounds.
>>
>> Maybe there's a better approach than flectioned. Consider the language
>> is frozen solid. How would you solve problems with it?
>
> I think the closest anyone has come is Jacob, with his orange library.
> Maybe he can respond to this point.
>
> -Steve
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.
More information about the Digitalmars-d
mailing list