Workaround for https://issues.dlang.org/show_bug.cgi?id=18422?

Adam D. Ruppe destructionator at gmail.com
Sun Feb 11 18:26:26 UTC 2018


On Sunday, 11 February 2018 at 15:34:07 UTC, Andrei Alexandrescu 
wrote:
> My intent is to have a struct Module, which can be initialized 
> with a module name; then:

The way I do this is to define the data structures kinda like you 
did, but then have a ctfe/template factory function to return it.

struct Module {
    string name;
    Data[] data;
   // etc
}

Module getModule(string name)() {
       Module mod;
       // populate here using template arg
       mod.name = name;
       mod.data = [__traits(allMembers, mixin(name))];
       // etc
       // return the simple struct
       return mod;
}


Then you can enum it individually:

enum x = getModule!"std.typecons";

and also runtime initialize it and put in an array:

Module[] allModules;

static this() {
    allModules ~= getModule!(__MODULE__);
}




This runtime data, compile time factory pattern I think solves 
everything you want without language changes. Is there anything 
about it you don't like?



More information about the Digitalmars-d mailing list