opDispatch and compile time parameters

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 19 12:53:13 PDT 2015


On 10/19/2015 02:26 PM, Adam D. Ruppe wrote:
> On Monday, 19 October 2015 at 18:16:15 UTC, Andrei Alexandrescu wrote:
>> Tangentially related: since when we allow field initialization with
>> new? I was surprised to see that this works:
>
> Since CTFE started supporting it... this might actually be an
> unintentional feature.
>
> Since the initializer is in a static context, the right hand side gets
> CTFE'd, which means that actually points to an array in the data
> segment... the *same* array in the data segment for all initializations
> (the pointer is just blitted over with the rest of init), which might be
> a bit surprising.
>
> I've seen a lot of people do this with classes not realizing it makes a
> static instance!

This works too:


struct A {
     int[] x = new int[10];
}

void main() {
     import std.stdio;
     A a;
     a.x[1] = 42;
     writeln(a.x);
}

Looks like a bona fide runtime array to me.


Andrei


More information about the Digitalmars-d mailing list