static foreach and new identifier names
biozic
dransic at gmail.com
Sat Jan 6 00:20:25 UTC 2018
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"
}
More information about the Digitalmars-d
mailing list