nested enum like template generator

Ali Çehreli acehreli at yahoo.com
Tue Jul 16 07:01:25 PDT 2013


On 07/15/2013 08:43 PM, JS wrote:

 > http://dpaste.dzfl.pl/7c8b0ba9
 >
 > Why the heck can't we use integers in ctfe's?

That is false:

import std.range;
import std.algorithm;
import std.conv;
import std.stdio;

string makeCode(int begin, int end)
{
     auto result = appender!string(`enum made = "`);

     iota(begin, end)
         .map!(a => a.to!string)
         .joiner(", ")
         .copy(result);

     result ~= `";`;

     return result.data;
}

unittest
{
     assert(makeCode(3, 7) == `enum made = "3, 4, 5, 6";`);
}

void main()
{
     mixin (makeCode(-3, 3));
     writeln(made);
}

 > There seems to be no
 > simple way to create a counter and this is one of the most basic
 > programming constructs to use..

That would be pretty limiting, right?

 > yet with ctfe's it's impossible.

I have a feeling that you had something else in mind.

 > I'd like each variable in the nested structs to be incremented properly.

It should be simple to convert the code above to do that.

Ali



More information about the Digitalmars-d-learn mailing list