How to move an associative array between modules?

WhatMeWorry kheaser at gmail.com
Fri Jan 12 20:46:50 UTC 2018


On Thursday, 11 January 2018 at 23:29:30 UTC, Adam D. Ruppe wrote:
> On Thursday, 11 January 2018 at 23:20:44 UTC, WhatMeWorry wrote:
>> When I simply move the array out of main() but still in app.d, 
>> the compiler returns
>> Error: expression ["SCRATCH":Track("scratch.wav", 
>> cast(Sound)1, 0, null),...  is not a constant.
>>
>> Can I use "static if" or "static this()", or "mixin" or some 
>> other technique?
>
> Yeah, just declare the array outside, then initialize it inside 
> a static this() constructor.
>
> int[int] foo;
> static this() {
>   foo = [1:1];
> }


I hate to keep being a bother, but my project with the below 
static this() now compiles fine, but aborts during runtime with a 
"a problem caused the program to stop working ..."

Does static if have some pitfalls I'm unaware of?

static this()
{
     tracks["SCRATCH"] = Track("scratch.wav", Sound.SOUND_EFFECT, 
ONCE,    null );

     // or this form code segment

     Track[string] tracks =
     [
         "SCRATCH" : Track("scratch.wav", Sound.SOUND_EFFECT, 
ONCE,    null )
     ];

}

I even tried just foo = [1:1]; but that crashed during run time.

I have a writeln() statement just after main() so I know it is 
occurring before main().




More information about the Digitalmars-d-learn mailing list