stringof and mixins when the types aren't available

Adam D. Ruppe destructionator at gmail.com
Sun Jun 16 18:09:24 PDT 2013


On Monday, 17 June 2013 at 00:36:40 UTC, David Nadlinger wrote:
> To expand on that a bit: In the case of default parameters, 
> instead of trying to directly generate a string representation

The thing here is I'm not really generating the string, this 
comes out of the compiler and I don't think it is available any 
other way.

void foo(int a, int b = 10) {}
pragma(msg, typeof(foo).stringof);

gives:

void(int a, int b = 10)


And from that, I read out the parameter names and the default 
values, but they are all as strings. So, to get it back to a 
value, I mixin(that slice of string)


The short version of what web.d tries to do is:

void callFunction(alias fun)(string[string] arguments) {
    ParameterTypeTuple!fun args;

    foreach(i, arg; args) {
         string arg_name = ParameterNames!(fun)[i] ;
         if(arg_name in arguments)
               args[i] = to!arg(arguments[arg_name]);
         else if(arg has default)
               args[i] = mixin(ParameterDefaults!(fun)[i]);
         else
              throw new Exception ("argument " ~ arg_name ~ " is 
missing");
    }

    fun(args);
}

that obviously won't compile cuz of the pseudocode, but that's 
more or less what I'm trying to do.


More information about the Digitalmars-d mailing list