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:
mixin(__ARGUMENTS__);
Here is the implementation:
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:
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.