Resolve function pointers using UDA during CT

Tim Volckmann timvol at ymail.com
Sun Feb 9 04:48:35 PST 2014


Hi guys,

is there any way to create an function-array during CT using UDAs 
like the following:

module myUdaFunctions;

struct MyUDA
{
    string Name;
}

@MyUDA("Function1")
string myFirstUdaFunction(string myString)
{
    // ... do something
}

@MyUDA("Function2")
string mySecondUdaFunction(string myString)
{
    // ... do something
}

/////////////////////

module myMain;

private
{
    string function(string)[string] callbacks;
}

void main(string[] args)
{
    // during ct:
    {
       // do something like this:
       //
       // auto u = __traits(getFunctionsByAttribute, "MyUDA")
       // foreach (myFunction; u)
       //    callbacks[myFunction.GetAttribute("MyUDA").Name] = 
myFunction
    }

    // during runtime:
    if (args[1] in callbacks)
    {
       callbacks[args[1]]("myString");
    }

}

Any way to do something like this?


More information about the Digitalmars-d-learn mailing list