January 06, 2018 Re: static foreach and new identifier names | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Friday, 5 January 2018 at 22:25:22 UTC, Adam D. Ruppe wrote:
>
> The idea is to take a list of inputs - functions with the @magic annotation - and create wrapper functions for them with the same name, just without the tailing _.
>
> (for one example)
Does this do what you want? (without UDA @magic)
import std.conv;
import std.meta;
import std.stdio;
import std.traits;
enum nameWithout_(alias a) = __traits(identifier, a)[0 .. $ - 1];
struct FunWrap {
static auto caller(alias func)(Parameters!func args) {
args[0]++;
return func(args);
}
}
static foreach (func; AliasSeq!(foo_, bar_)) {
mixin("alias " ~ nameWithout_!func ~ " = FunWrap.caller!func;");
}
private string foo_(int i) {
return "foo says " ~ i.to!string;
}
private string bar_(int i) {
return "bar says " ~ i.to!string;
}
void main() {
writeln(foo(42)); // "foo says 43"
writeln(bar(0)); // "bar says 1"
}
|
Copyright © 1999-2021 by the D Language Foundation