Obtaining argument names in (variadic) functions

JR via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 17 06:53:00 PDT 2016


On Thursday, 17 March 2016 at 11:52:13 UTC, Edwin van Leeuwen 
wrote:
> On Wednesday, 16 March 2016 at 20:53:42 UTC, JR wrote:
>>
>>
>> void printVars(Args...)()
>> if (Args.length > 0)
>> {
>>     import std.stdio : writefln;
>>
>>     foreach (i, arg; Args) {
>>         writefln("%s\t%s:\t%s", typeof(Args[i]).stringof, 
>> Args[i].stringof, arg);
>>     }
>> }
>>
>> void main() {
>>     int abc = 3;
>>     string def = "58";
>>     float ghi = 3.14f;
>>     double jkl = 3.14;
>>
>>     printVars!(abc,def,ghi,jkl)();
>> }
>
> Interesting, any idea if it is possible to do assignment within 
> template.. Either:
>
> printVars!(int abc=5,string def="58")();
> or something like
> printVars!("abc","def",ghi)(5,"58");

What would the use-cases for those be?

I don't think the first is valid grammar, and I'm not sure what 
you want the second to do. Resolve symbols by string literals of 
their names? That might need a string mixin as they wouldn't be 
in scope when in the called template function, but I've never 
tried it.

You *can* cook up something that modifies the values of variables 
you pass in -- like modifyVars!(abc,def,ghi)("asdf", 123, 3.14) 
-- but you just might be better off with runtime ref parameters 
then.


More information about the Digitalmars-d-learn mailing list