UDA magic?

evilrat evilrat666 at gmail.com
Thu Apr 17 02:39:00 PDT 2014


Greetings,

it is possible to put function as attribute, but is this possible 
to get "subject" of UDA's? so combining both could we should get 
something like this:
----------------
// module scope or static struct member
int[Variant] values; // should be typeinfo?

void registerStructVal(alias val)()
{
   alias symbol = ... some pragma or trait to get struct S ... // 
not any yet?
   static if ( is(symbol == struct) )
   {
     values[val] = Variant(symbol);
   }
}

@(registerStructVal!42())
struct S {}

@(registerClassVal!3())
class C
{
int x;
this(int val) { x = val; }
}


void main()
{
writeln(values); // 42 = S, 3 = C
auto a = values[3](123); // ok for class there typeinfo.create, 
but how to call specific ctor using it?
writeln(a.x) // 123
}
----------------

it may be hacked some way like defining any specific UDA as tag, 
use module ctor or template to inject code into structs with that 
tag, or maybe other ways which would be intrusive. what i'm 
looking for is to be able declare such function/template 
non-intrusive way.


More information about the Digitalmars-d-learn mailing list