Thread overview
How to move an associative array between modules?
Jan 11, 2018
WhatMeWorry
Jan 11, 2018
Adam D. Ruppe
Jan 12, 2018
WhatMeWorry
Jan 12, 2018
H. S. Teoh
January 11, 2018
I've built a sound.d module with lots data types, free functions (initAndOpenSound() loadSound()), and enums etc.

In my main/app.d module, I've created the the following associative array:

void main(string[] argv)
{
    initAndOpenSound();

    Track[string] tracks  =
    [
        "SCRATCH"          : Track("scratch.wav", Sound.SFX,   ONCE,    null ),
        "BACKGROUND_SOUND" : Track("beat.wav",    Sound.MUSIC, FOREVER, null ),
        "HIGH"             : Track("high.wav",    Sound.SFX,   ONCE,    null )	
    ];
	
    foreach(string s, Track t; tracks)
    {
        loadSound(t);   // sets ptr to a file with audio data
        tracks[s] = t;  // update associative array
    }


Everything works.  But I know in the future, that the tracks is going to get very large.

So I want to get it out of main() and move it to the sound.d module.  I've tried this but all hell breaks loose and I can't make just of the compiler errors


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?

Or do I need to refactor my code completely?



January 11, 2018
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];
}

January 12, 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().


January 12, 2018
On Fri, Jan 12, 2018 at 08:46:50PM +0000, WhatMeWorry via Digitalmars-d-learn wrote: [...]
> 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 ..."

Is there a way to get at the actual error message? That would be immensely more helpful in diagnosing the problem.

One potential pitfall of static this(), though unlikely given your
circumstances, is if there are multiple modules that use static this()
and there's a circular dependency somewhere.  If this is the problem,
splitting the static this() into a separate module may help.

If all else fails, you *could* just move the initialization code into
main() (or a function called by main()).  It's uglier, but should get
things going again until you have more time to investigate how to fix
the problem.


T

-- 
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth