DIP 1030--Named Arguments--Community Review Round 1 Discussion
Timon Gehr
timon.gehr at gmx.ch
Tue Feb 11 09:02:03 UTC 2020
On 10.02.20 20:46, Adam D. Ruppe wrote:
> 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.
What if fn is a template?
More information about the Digitalmars-d
mailing list