www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there a way to get this associative array initialization to work?

reply WhatMeWorry <kheaser gmail.com> writes:
enum SoundType { MUSIC = 0, SOUND_EFFECT };

struct Sound
{
     string     file;
     SoundType  musicOrSfx;
     void*      ptr;   // Mix_Chunk* for sfx;  Mix_Music* for 
music;
}

immutable Sound[string] soundLibrary  =   // line 148
[
     "SCRATCH"          : { file : "scratch.wav", musicOrSfx : 
SOUND_EFFECT, ptr : null },
     "BACKGROUND_TRACK" : { file : "beat.wav",    musicOrSfx : 
MUSIC,        ptr : null },
     "HIGH"             : { file : "high.wav",    musicOrSfx : 
SOUND_EFFECT, ptr : null }	
];	


I keep getting a
source\app.d(148,1): Error: not an associative array initializer
Jan 09
next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 1/9/18 6:05 PM, WhatMeWorry wrote:
 enum SoundType { MUSIC = 0, SOUND_EFFECT };
 
 struct Sound
 {
      string     file;
      SoundType  musicOrSfx;
      void*      ptr;   // Mix_Chunk* for sfx;  Mix_Music* for music;
 }
 
 immutable Sound[string] soundLibrary  =   // line 148
 [
      "SCRATCH"          : { file : "scratch.wav", musicOrSfx : 
 SOUND_EFFECT, ptr : null },
      "BACKGROUND_TRACK" : { file : "beat.wav",    musicOrSfx : 
 MUSIC,        ptr : null },
      "HIGH"             : { file : "high.wav",    musicOrSfx
: 
 SOUND_EFFECT, ptr : null }
 ];
 
 
 I keep getting a
 source\app.d(148,1): Error: not an associative array initializer
 
 
https://issues.dlang.org/buglist.cgi?list_id=218715&longdesc=not%20an%20associative%20array%20initializer&longdesc_type=allwordssubstr&query_format=advanced&resolution=--- Note, I think you need at least Sound.SOUND_EFFECT, etc. This bug looks particularly relevant: https://issues.dlang.org/show_bug.cgi?id=11221 I think it should really work. -Steve
Jan 09
parent reply Jacob Carlborg <doob me.com> writes:
On 2018-01-10 00:19, Steven Schveighoffer wrote:

 https://issues.dlang.org/buglist.cgi?list_id=218715&longdesc=not%20an%20associative%20array%20initializer&longdesc_type=allwordssubstr&query_format=adv
nced&resolution=--- 
 
 
 Note, I think you need at least Sound.SOUND_EFFECT, etc.
 
 This bug looks particularly relevant: 
 https://issues.dlang.org/show_bug.cgi?id=11221
 
 I think it should really work.
It's not just in associative arrays it doesn't work. It basically only works in variables declarations: https://issues.dlang.org/show_bug.cgi?id=15692 -- /Jacob Carlborg
Jan 10
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 1/10/18 10:11 AM, Jacob Carlborg wrote:
 On 2018-01-10 00:19, Steven Schveighoffer wrote:
 
 https://issues.dlang.org/buglist.cgi?list_id=218715&longdesc=not%20an%20associative%20array%20initializer&longdesc_type=allwordssubstr&query_format=adv
nced&resolution=--- 


 Note, I think you need at least Sound.SOUND_EFFECT, etc.

 This bug looks particularly relevant: 
 https://issues.dlang.org/show_bug.cgi?id=11221

 I think it should really work.
It's not just in associative arrays it doesn't work. It basically only works in variables declarations: https://issues.dlang.org/show_bug.cgi?id=15692
Right, but it doesn't work in variable declarations if that variable is part of an associative array. In other words: struct S { int x; } struct T { S s; } T t = {s: {x: 1}}; // nesting works here S[] arr = [{x: 1}, {x:2}]; // and here S[string] aa = ["first": {x: 1}, "second": {x: 2}]; // but not here??? -Steve
Jan 10
parent Jacob Carlborg <doob me.com> writes:
On 2018-01-10 17:29, Steven Schveighoffer wrote:

 Right, but it doesn't work in variable declarations if that variable is 
 part of an associative array.
 
 In other words:
 
 struct S
 {
     int x;
 }
 
 struct T
 {
     S s;
 }
 
 T t = {s: {x: 1}}; // nesting works here
 S[] arr = [{x: 1}, {x:2}]; // and here
 S[string] aa = ["first": {x: 1}, "second": {x: 2}]; // but not here???
Aha, I see. Didn't expect any of those to work. -- /Jacob Carlborg
Jan 10
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
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.
Jan 09