Is there a way to get this associative array initialization to work?

Adam D. Ruppe destructionator at gmail.com
Tue Jan 9 23:20:09 UTC 2018


On Tuesday, 9 January 2018 at 23:05:21 UTC, WhatMeWorry wrote:
> source\app.d(148,1): Error: not an associative array initializer

The C-style struct initialization in there is a problem. Try 
making it

immutable Sound[string] soundLibrary  =   // line 148
[
     "SCRATCH"          : Sound("scratch.wav", 
SoundType.SOUND_EFFECT, null ),
     "BACKGROUND_TRACK" : Sound("beat.wav",    SoundType.MUSIC,    
null ),
     "HIGH"             : Sound("high.wav",    
SoundType.SOUND_EFFECT, null
     )
];

using positional values instead of named. Moreover, note that 
such initialization needs to be done in a function, not at global 
scope. you may need to declare it outside then initialize it in a 
static constructor.


More information about the Digitalmars-d-learn mailing list