On 02.02.2016 09:27, Robert M. Münch wrote:

==> BEGIN
[...]
enum A {afoo, bfoo, cfoo};

(Aside: In D no semicolon is needed here.)

string generateEnums(T...)(string type){
    string code = "enum " ~ type ~ " {";

    // this is a static foreach (compile time)
    foreach(m; T){
      debug pragma(msg, m ~ ","); // check what code we get at compile time
      code ~= m ~ ",";
    }

    return(code ~ "}");

(Aside: Those parentheses are misleading. return is not a function.)

}

int main(){
[...]
    mixin(generateEnums!members1("B"));
    B switch_var_b = chomp(user_input).to!B; // get rid of terminating
chars

[...]
}

<== END

I'm not saying that everything is perfect as it is, but that code can be made nicer with what we have right now:

----
template generateEnums(string[] members)
{
    // 'join' variant:
    import std.array: join;
    mixin("enum generateEnums {" ~ members.join(",") ~ "}");

    // 'format' variant:
    // import std.format;
    // mixin(format(q{ enum generateEnums {%-(%s, %)} }, members));
}

void main()
{
    alias B = generateEnums!([members1]);
    B switch_var_b = chomp(readln()).to!B;
    /* ... */
}
----

How about being able to write something like "ensure_final_switch B;"
and have this call a CTF that generates the necessary code and has
access to tool for building D structured code, AST etc.? And has a
compile-time state I can later access in a upcoming CTF.

So you're asking for AST macros, I suppose. There are two DIPs for them:

http://wiki.dlang.org/DIP50 - AST Macros
http://wiki.dlang.org/DIP78 - AST Macros Lite

I don't know where they stand, as I'm not really interested in the whole thing, but maybe one of those matches your vision.

If that's not what you have in mind, please be more concrete about what you think of. Maybe show some pseudo code showing how you'd like to be able to solve the example of generating enums.

Reply via email to