Obtaining argument names in (variadic) functions

Edwin van Leeuwen via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 17 07:12:38 PDT 2016


On Thursday, 17 March 2016 at 13:53:00 UTC, JR wrote:
>>
>> 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.

Both use cases are when you want a named parameter, without 
having to assign it first. I know the first is not valid grammar, 
was just wondering if you might be smarter than me and see a way 
to make it valid :)

Second one is another possible alternative that I have been 
thinking about.

Basically, say I want to have the named (optional) parameters x 
and y. In your initial example I would be required to do:

```
int x = 1;
string y = "2";
doSomethingWithNamedPars!(x,y)();
```

I just hoped to shorten that to a one liner similar to:

```
doSomethingWithNamedPars!(x=1,y="2")();
```

or alternatively

```
doSomethingWithNamedPars!("x","y")(1,"2");
```

(where doSomethingWithNamedPars's behaviour depends on which 
named parameters it is passed)

Just as a reference, my current approach (in ggplotd) is with 
named tuples, but that is slightly more verbose than I had hoped:

```
doSomethingWithNamedPars( Tuple!(int, "x", string, "y")( 1, 2 ) );
```


More information about the Digitalmars-d-learn mailing list