On Thursday, 17 March 2016 at 14:12:38 UTC, Edwin van Leeuwen wrote:
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 ) );
```

http://dpaste.dzfl.pl/1625122e4f01 is the best I can do, and it's ugly. It's something akin to your doSomethingWithNamedPars!("x","y")(1,"2"). I may even have missed the point.

It *neccessitates* that the called function knows the parameter names, because they're not self-documented in the signature. It will also have to deal with any missing ones to actually compile.

I'm not sure how to do this and still enjoy things like const and ref and friends.

I imagine there *might* be a way to do this with normal non-templated functions given a similar wrapper, if the functions are annotated so that it can get the names of the parameters. It feels like you should be able to get it to naïvely reorder the arguments, default-initialise any missing ones, etc. But you quickly run into "xyz cannot be read at compile-time", even when it feels like everything should be known.

Reply via email to