Trait or builtin that returns arguments passed to current function (if any)
Adam D Ruppe
destructionator at gmail.com
Thu Sep 9 14:35:42 UTC 2021
On Thursday, 9 September 2021 at 13:49:55 UTC, Alexandru Ermicioi
wrote:
> You can already get the parameter list of a function using is
> expression.
parameters are not arguments.
> But it is a bit buggy, since you have to play a bit with it,
> before you can even extract the values.
the is expression works perfectly fine, it is just a different
thing. parameters are static information about the function's
outside. arguments are dynamic data inside the function.
You can get to arguments through parameters though like I
mentioned in the link in the OP though. A reasonable
implementation is this:
---
public static import std.meta;
template ARGS(alias a /* always pass the first argument to your
function to this */) {
string helper() {
string code = "std.meta.AliasSeq!(";
static if(is(typeof(__traits(parent, a)) Params
== __parameters))
foreach(idx, param; Params) {
if(idx)
code ~= ", ";
code ~= __traits(identifier, Params[idx
.. idx + 1]);
}
else static assert(0);
code ~= ")";
return code;
}
enum string ARGS = helper();
}
---
And then you can use it like:
---
import std.stdio;
void foo(int a) {
foreach(item; mixin(ARGS!(a)))
writeln(item);
// forwarding can be done like this too:
foo("omg", mixin(ARGS!a));
}
void foo(string a, int b) {
foreach(item; mixin(ARGS!(a)))
writeln(item);
}
void main() {
foo(4);
foo("omg", 55);
}
---
This implementation doesn't work on variadic templates but that's
not so important since they are already a tuple.
Still though, it just would be nicer if it just worked with a
builtin.
More information about the Digitalmars-d
mailing list