Replace (ie: substitute) a type in varadic args

Saurabh Das via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 2 01:20:22 PDT 2016


On Tuesday, 2 August 2016 at 08:16:48 UTC, Sean Campbell wrote:
> On Tuesday, 2 August 2016 at 07:24:28 UTC, Saurabh Das wrote:
>> [...]
>
> Just of the top of my head, using ugly string mixins, this:
> auto myConverterFunc(Args...)(Args args)
> {
> 	string genCode()
> 	{
> 	    string code = "targetFunction(";
> 	    foreach (i, Arg; Args)
> 	    {
> 	        static if (is(Arg == bool))
> 	            code ~= format("cast(ubyte)args[%s]%s", i, i == 
> Args.length ? "" : ",");
> 	        else
> 	            code ~= format("args[%s]%s", i, i == Args.length ? 
> "" : ",");
> 	    }
> 	    code ~= ");";
> 	    return code;
> 	}
>     mixin(genCode());
> }
>
> void targetFunction(ubyte i, ubyte j, uint k, int l)
> {
> 	writefln("i : %s, j : %s, k : %s, l : %s",i,j,k,l);
> }
>
> void main()
> {
> 	myConverterFunc(true,false,10,20);
> }

Thanks. Yes that is one approach. I figured out another approach 
that seems decent:

auto targetFunctionProxy(Args...)(Args args)
{
     import std.meta;
     return targetFunction!(ReplaceAll!(bool, ubyte, Args))(args);
}


More information about the Digitalmars-d-learn mailing list