How to move an associative array between modules?

WhatMeWorry kheaser at gmail.com
Thu Jan 11 23:20:44 UTC 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?





More information about the Digitalmars-d-learn mailing list