DIP 1030--Named Arguments--Community Review Round 1 Discussion

Adam D. Ruppe destructionator at gmail.com
Mon Feb 10 19:46:29 UTC 2020


On Monday, 10 February 2020 at 19:38:34 UTC, Manu wrote:
> If we must mirror
> the argument names and default arguments, you must fall back 
> into string mixin territory

It is super simple. Consider this example:

---
int foo(string s = null, int a = 12) {
         return a + 12;
}
template wrapWithPrint(alias fn) {
         static if(is(typeof(fn) P == __parameters))
         auto wrapWithPrint(P params) {
                 import std.stdio;
                 writeln(params);
                 auto ret = fn(params);
                 writeln("returned: ", ret);
                 return ret;
         }
         else static assert(0);
}
void main() {
         wrapWithPrint!foo();
         wrapWithPrint!foo("cool");
         wrapWithPrint!foo("cool", 20);
}
---

Default values and param names are included in the __parameters 
thing and just works if you use it directly.

I saw people overcomplicating this just a few hours ago too which 
is why I have this example ready. There's plenty of techniques to 
do this though I guess they aren't well known.


More information about the Digitalmars-d mailing list