On Saturday, 11 May 2019 at 15:48:44 UTC, Bogdan wrote:
What would be the most straight-forward way of mapping the
members of an enum to the members of another enum (one-to-one
mapping) at compile time?
An example of a Initial enum that creates a derived enum using
the same element names but applying a transformation via a
function foo() pus adding some other enum elements in the Derived
one not present in the Initial.
It's a little bit clumsy but works very well.
I use this at module level. This allows to have the Derived enum
at compile time so that it can be used to declare variables or
functions at compile time.
mixin({
string code = "enum Derived : ulong { "~
"init = 0,"; /* We set the dummy
init value to 0 */
static foreach(i; __traits(allMembers, Initial)) {
code ~= i~"= foo(Initial."~i~"),";
}
code ~= "
ALL = Whatever,
THING = 42,
return code ~ "}";
}());