SList and DList init doesn't work in global variable

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 28 14:37:48 PDT 2015


On 09/28/2015 02:15 PM, Martin Krejcirik wrote:
> Is this intended or known issue ? It works with 2.066.
>
> SList!int gslist = [1,2,3,4,5,6]; // broken since 2.067
> // Error: reinterpreting cast from NodeWithoutPayload* to Node* is not
> supported in CTFE
>
> DList!int gdlist = [1,2,3,4,5,6]; // broken since 2.067
> // Error: non-constant expression ...
>
> void main()
> {
>      SList!int lslist = [1,2,3,4,5,6]; // OK
>      DList!int ldlist = [1,2,3,4,5,6]; // OK
> }
>

Yes, it looks like there are problems with their implementations. Those 
questions aside, I would initialize such module variables in a module 
static this() anyway:

import std.container;

SList!int gslist;
DList!int gdlist;

static this() {
     gslist = [1,2,3,4,5,6];
     gdlist = [1,2,3,4,5,6];
}

void main() {
}

If you want the lists to be immutable, then the following initialization 
pattern works:

import std.container;

immutable SList!int gslist;
immutable DList!int gdlist;

auto init_gslist()
{
     return SList!int([1,2,3,4,5,6]);
}

auto init_gd_list()
{
     return DList!int([1,2,3,4,5,6]);
}

static this() {
     gslist = init_gslist();
     gdlist = init_gd_list();
}

void main() {
}

The fact that I needed the two module-level init_* functions is also 
problematic, right? For example, moving those inside static this() does 
not work.

Ali



More information about the Digitalmars-d-learn mailing list