Compile time mapping
    Patrick Schluter 
    Patrick.Schluter at bbox.fr
       
    Sun May 12 16:32:35 UTC 2019
    
    
  
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 ~ "}";
}());
    
    
More information about the Digitalmars-d-learn
mailing list