nested enum like template generator

Ali Çehreli acehreli at yahoo.com
Tue Jul 16 14:12:36 PDT 2013


On 07/16/2013 02:01 PM, Ali Çehreli wrote:

 > On 07/16/2013 01:40 PM, JS wrote:

 >  > It would be nice if we had some way to data globally(in module).
 >  >
 >  > e.g., __ctfestore["name"] = value;
 >
 > I would expect model-level objects start their lives after the program
 > starts running but their initial value can be calculated during compile
 > time:
 >
 > import std.stdio;
 > import std.conv;
 >
 > int[string] ctfestore;
 >
 > static this()
 > {
 >      ctfestore = A!().globalFunc();
 > }

Ok, I've been silly. That's not CTFE. I meant something like this:

static this()
{
     enum initialValue = A!().globalFunc();
     ctfestore = initialValue;
}

And only then I got the problem:

 > template A()
 > {
 >      int c;
 >
 >      int[string] globalFunc()
 >      {
 >          int[string] result;
 >
 >          void func()
 >          {
 >              for ( ; c < 10; ++c) {

Error: static variable c cannot be read at compile time
        called from here: func()
        called from here: globalFunc()

 >                  result[c.to!string] = c;
 >              }
 >          }
 >
 >          func();
 >          return result;
 >      }
 > }
 >
 > void main()
 > {
 >      writeln(ctfestore);
 > }
 >
 > Prints:
 >
 > ["0":0, "4":4, "8":8, "1":1, "5":5, "9":9, "2":2, "6":6, "3":3, "7":7]

Ali



More information about the Digitalmars-d-learn mailing list