Trait or builtin that returns arguments passed to current function (if any)

bauss jj_1337 at live.dk
Thu Sep 9 08:39:05 UTC 2021


On Wednesday, 8 September 2021 at 21:53:21 UTC, Per Nordlöw wrote:
> Adam, and like others including me, are longing for a 
> trait/builtin [1] that returns the parameters passed to the 
> function of the current scope (if any). Has such a thing been 
> proposed previously?
>
> [1] 
> http://dpldocs.info/this-week-in-d/Blog.Posted_2021_07_26.html#on-my-wish-list

While there isn't something directly that does it then it's 
fairly trivial to implement.

All you have to do to use it then is calling:

```d
mixin(__ARGUMENTS__);
```

Here is the implementation:

```d
string __ARGUMENTS__(string fn = __FUNCTION__)()
{
     import std.traits : ParameterIdentifierTuple;
     import std.array : join;

     mixin("enum parameterNames = [ParameterIdentifierTuple!(" ~ 
fn ~ ")];");

     return "auto __arguments = [" ~ parameterNames.join(",") ~ 
"];";
}
```

Here is a working example:

```d
string __ARGUMENTS__(string fn = __FUNCTION__)()
{
     import std.traits : ParameterIdentifierTuple;
     import std.array : join;

     mixin("enum parameterNames = [ParameterIdentifierTuple!(" ~ 
fn ~ ")];");

     return "auto __arguments = [" ~ parameterNames.join(",") ~ 
"];";
}

void test(int x, int y)
{
     import std.stdio;

     mixin(__ARGUMENTS__);

     auto c = __arguments[0];
     auto d = __arguments[1];

     writefln("%d %d", c, d);
}

void main()
{
     test(1,2);
}
```
Output:
1 2

But yeah it would be nice if we could have it built-in, which I 
don't think has been done.


More information about the Digitalmars-d mailing list