Open burner opened 16 years ago
This is documented, and hence an enhancement request: "An AssocArrayLiteral cannot be used to statically initialize anything."
http://www.digitalmars.com/d/1.0/expression.html#AssocArrayLiteral
Reduced test case:
static int sizemap[int]= [0:0, 1:1, 2:4, 3:8, 4:8, 4:16];
changed title from "const initializer rejected".
Issue 6238 has been marked as a duplicate of this issue.
Came here from issue 6238
10.5 years and this is still a problem. Encountered when trying to assign AA literal to a struct member.
See https://run.dlang.io/is/BDVjxP for another example.
Issue 5279 has been marked as a duplicate of this issue.
(In reply to Don from comment #2)
Reduced test case:
static int sizemap[int]= [0:0, 1:1, 2:4, 3:8, 4:8, 4:16];
changed title from "const initializer rejected".
The work-around is:
enum int[int] sizemap= [0:0, 1:1, 2:4, 3:8, 4:8, 4:16];
BTW, this is a very old issue, if it's not intended to be fixed, can we add to the doc, and close it?
(In reply to mw from comment #6)
The work-around is:
enum int[int] sizemap= [0:0, 1:1, 2:4, 3:8, 4:8, 4:16];
That's not a workaround at all. That's defining a constant. My example from 2018 even illustrates exactly this, and that assigning the constant to a static variable does not work.
davidl reported this on 2007-10-13T07:58:08Z
Transfered from https://issues.dlang.org/show_bug.cgi?id=1578
CC List
Description
const MTBit8 = 1; const MTBit16 = 2; const MTBit32 = 3; const MTBit64 = 4; const MTBit80 = 4; const MTBit128 = 5; static int sizemap[int]=[0:0,MTBit8:byte.sizeof, MTBit16:int.sizeof, MTBit32:8, MTBit64:long.sizeof, MTBit80:16];