Compile-time function call with transformed parameters

realhet real_het at hotmail.com
Wed Oct 14 23:33:39 UTC 2020


Hi,
Is there a way to make this in a nicer way without the string 
mixin?

private auto generateVector(CT, alias fun, T...)(in T args){
   static if(anyVector!T){
     Vector!(CT, CommonVectorLength!T) res;
     static foreach(i; 0..res.length) res[i] = mixin("fun(", 
T.length.iota.map!(j => 
"args["~j.text~"].vectorAccess!i").join(','), ")");
     return res;
   }else{
     return fun(args);
   }
}

The mixin expands to:
fun(args[0].vectorAccess!i, ..., args[$-1].vectorAccess!i)

And vectorAccess is a function with a template int parameter (i).

private auto vectorAccess(int idx, T)(in T a){
   static if(isVector!T) return a[idx];
                    else return a;
}

I tried with staticMap, but that vectorAccess is a runtime thing. 
Other than that I have no idea. I'm 99% sure DLang has a nice 
thing for this, but can't find it yet.

Maybe it is possible to put a thing like this -> [ 
args[0].vectorAccess!i ] into an AliasSeq and then give that 
inside the fun(), but how can I generate effectively an array of 
code?

Thank you!



More information about the Digitalmars-d-learn mailing list