digitalmars.D.learn - Is there a way to get this associative array initialization to work?
- WhatMeWorry (19/19) Jan 09 2018 enum SoundType { MUSIC = 0, SOUND_EFFECT };
- Steven Schveighoffer (7/31) Jan 09 2018 https://issues.dlang.org/buglist.cgi?list_id=218715&longdesc=not%20an%20...
- Jacob Carlborg (6/15) Jan 10 2018 It's not just in associative arrays it doesn't work. It basically only
- Steven Schveighoffer (16/33) Jan 10 2018 Right, but it doesn't work in variable declarations if that variable is
- Jacob Carlborg (4/22) Jan 10 2018 Aha, I see. Didn't expect any of those to work.
- Adam D. Ruppe (17/18) Jan 09 2018 The C-style struct initialization in there is a problem. Try
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 2018
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 initializerhttps://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 2018
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 2018
On 1/10/18 10:11 AM, Jacob Carlborg wrote:On 2018-01-10 00:19, 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??? -Stevehttps://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
Jan 10 2018
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 2018
On Tuesday, 9 January 2018 at 23:05:21 UTC, WhatMeWorry wrote:source\app.d(148,1): Error: not an associative array initializerThe 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 2018