Initialising arrays at compile time

Peter Alexander peter.alexander.au at gmail.com
Sun Jan 2 06:07:54 PST 2011


Ok, someone put me out of my misery, I can't figure out for the life of 
me how to do this. Basically, I have a vector class and want enums for 
the zero vectors:

struct Vec
{
   this(real x, real y) { e[0] = x; e[1] = y; }
   real[2] e;
   enum Vec zero = Vec(0, 0);
}

What can I do?

The above doesn't work because:

test.d(3): Error: Index assignment this.e[0u] = x is not yet supported 
in CTFE
test.d(3): Error: Index assignment this.e[1u] = y is not yet supported 
in CTFE


Changing the constructor to do e = [x, y] doesn't work either.

test.d(3): Error: Slice operation this.e[] = [x,y] cannot be evaluated 
at compile time
test.d(5): Error: cannot evaluate __ctmp1.this(0L,0L) at compile time
test.d(5): Error: cannot evaluate __ctmp1.this(0L,0L) at compile time


What can I do?

And once I do that, I need to do it also for:

struct Vec(int N)
{
   real[N] e;
   ...
}

Thanks in advance.


More information about the Digitalmars-d-learn mailing list