www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - bug in doc?

reply spir <denis.spir gmail.com> writes:
https://dlang.org/spec/hash-map.html#static_initialization:

immutable long[string] aa = [
   "foo": 5,
   "bar": 10,
   "baz": 2000
];

==> Error: non-constant expression `["foo":5L, "bar":10L, "baz":2000L]`

Also: I don't understand the error message:
* What is non-constant in the *expression*?
* And why should it be constant at all?
(Removing "immutable" does not help...)

diniz
Mar 14 2019
next sibling parent Andrea Fontana <nospam example.org> writes:
On Thursday, 14 March 2019 at 14:22:52 UTC, spir wrote:
 https://dlang.org/spec/hash-map.html#static_initialization:

 immutable long[string] aa = [
   "foo": 5,
   "bar": 10,
   "baz": 2000
 ];
If I'm right, you can't use this syntax with global array. Insted this works: void main() { immutable long[string] aa = [ "foo": 5, "bar": 10, "baz": 2000 ]; } You should init global AAs using static this() { } as explained in the same doc Andrea
Mar 14 2019
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 14 March 2019 at 14:22:52 UTC, spir wrote:
 https://dlang.org/spec/hash-map.html#static_initialization:
Well, bug in implementation. That is *supposed* to work, but the compiler never implemented it. The docs really should point out this fact explicitly, though.
Mar 14 2019
parent Daniel N <no public.email> writes:
On Thursday, 14 March 2019 at 14:47:18 UTC, Adam D. Ruppe wrote:
 On Thursday, 14 March 2019 at 14:22:52 UTC, spir wrote:
 https://dlang.org/spec/hash-map.html#static_initialization:
Well, bug in implementation. That is *supposed* to work, but the compiler never implemented it. The docs really should point out this fact explicitly, though.
Especially since it's hard to workaround inside function scope.
Mar 14 2019