On Thursday, 19 February 2015 at 07:46:51 UTC, Ali Çehreli wrote:
On 02/18/2015 10:39 PM, stewarth wrote:
> This works under dmd 2066.1 but fails under dmd 2.067-b2.
I don't know whether it is a bug.
> struct B {
> A* a;
In any case, that must be immutable(A)*.
> }
>
> static immutable B[] someB = [{a:&someA[0]}, {a:&someA[1]}];
> I want it to initialize at runtime before main(). I don't
> actually want any CTFE stuff here.
Then you need 'static this()' (or 'shared static this()'):
static immutable B[] someB;
static this() {
someB = [ B(&someA[0]), B(&someA[1]) ];
}
Note that I could not use the named member syntax because in my
case the compiler cannot know what the right-hand side
expression is. However, it is still possible with a temporary
where the type is explicit as in your case:
static this() {
immutable B[] tmp = [ {a:&someA[0]}, {a:&someA[1]} ];
someB = tmp;
}
Ali
Hi Ali,
Thanks for the help.
I've gone with "static this()" approach and it works. In a way
it's cleaner because it's explicit that these variables are only
initialised at runtime before d_main(). At least that's how I
understand things :)
It would be nice if the named syntax also worked in static
this(), maybe I'll file an ER for it. I'm a big fan of the whole
named args thing in Python, which from a quick search has been
discussed before in the forums.
Cheers,
Stew