Bug 1578 – Allow AA literals to initialize static variables

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2007-10-13T07:58:08Z
Last change time
2023-10-29T01:31:04Z
Assigned to
No Owner
Creator
david

Comments

Comment #0 by davidl — 2007-10-13T07:58:08Z
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];
Comment #1 by matti.niemenmaa+dbugzilla — 2007-10-13T08:21:12Z
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
Comment #2 by clugdbug — 2012-09-27T01:14:26Z
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".
Comment #3 by yebblies — 2015-04-11T03:39:03Z
*** Issue 6238 has been marked as a duplicate of this issue. ***
Comment #4 by gooberman — 2018-05-20T19:35:59Z
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.
Comment #5 by dmitry.olsh — 2018-05-22T09:08:59Z
*** Issue 5279 has been marked as a duplicate of this issue. ***
Comment #6 by mingwu — 2021-04-12T21:42:02Z
(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?
Comment #7 by gooberman — 2021-04-12T22:09:58Z
(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.
Comment #8 by schveiguy — 2023-10-29T01:30:55Z
This will be fixed in 2.106, we must have missed this one when merging the fix. https://github.com/dlang/dmd/pull/15468