Inspired by https://forum.dlang.org/post/[email protected]
It would be nice if phobos had a CopyParameters template, which copies both parameter types and -names, such that this compiles:
void fun(int a, string s);
void gun(mixin CopyParameters!fun) {
a = 3;
s = "foo";
}
When I do this today, the error message says 'basic type expected, not mixin'.
The feature should for string mixins: void foo(mixin("int x")) {}
And for template mixins: void bar(mixin tmp!()) {}
template tmp() {
int a;
}
Comment #1 by nick — 2023-07-31T11:59:10Z
Just to mention, you can copy parameters like this:
```d
// simpler version of std.traits.Parameters (which also works)
template Params(alias func)
{
static if (is(typeof(func) PS == __parameters))
alias Params = PS;
}
void f(Params!g, int extra);
void g(char c = 0, byte b);
pragma(msg, typeof(f));
```
If you need just one parameter, use slicing `PS[i..i+1]` rather than indexing and it will preserve not just the type but the identifier and default argument too.
Comment #2 by nick — 2023-07-31T12:46:50Z
Actually std.traits.Parameters does not work, it is just the parameter types.
The pragma above prints:
void(char c = '\0', byte b, int extra)
(Even though the default argument can't be followed by non-default args!).
Comment #3 by robert.schadek — 2024-12-13T19:00:50Z