Re: Cannot initialize associative array

2018-01-21 Thread Jacob Carlborg via Digitalmars-d-learn
On 2018-01-20 00:16, rumbu wrote: According to this (https://dlang.org/spec/hash-map.html#static_initialization) this is correct static initialization for AA: immutable RoundingMode[string] ibmRounding = [     ">" : RoundingMode.towardPositive,     "<" : RoundingMode.towardNegative,     "0

Re: Cannot initialize associative array

2018-01-20 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, January 19, 2018 23:39:08 rumbu via Digitalmars-d-learn wrote: > Thank you Adam, just figured out myself the same solution, but I > didn't expect to have a static constructor in main.d. I thought > static constructors are meant to be used in imported modules. > Thanks again. There reall

Re: Cannot initialize associative array

2018-01-19 Thread rumbu via Digitalmars-d-learn
On Friday, 19 January 2018 at 23:27:06 UTC, Adam D. Ruppe wrote: On Friday, 19 January 2018 at 23:16:19 UTC, rumbu wrote: According to this (https://dlang.org/spec/hash-map.html#static_initialization) this is correct static initialization for AA: That only works inside a function, and, ironic

Re: Cannot initialize associative array

2018-01-19 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 19 January 2018 at 23:16:19 UTC, rumbu wrote: According to this (https://dlang.org/spec/hash-map.html#static_initialization) this is correct static initialization for AA: That only works inside a function, and, ironically, only if the variable is not `static`... I believe this is

Cannot initialize associative array

2018-01-19 Thread rumbu via Digitalmars-d-learn
According to this (https://dlang.org/spec/hash-map.html#static_initialization) this is correct static initialization for AA: immutable RoundingMode[string] ibmRounding = [ ">" : RoundingMode.towardPositive, "<" : RoundingMode.towardNegative, "0" : RoundingMode.towardZero, "=0":

Re: Cannot initialize associative array.

2010-06-24 Thread Andrej Mitrovic
Andrej Mitrovic Wrote: > Bernard Helyer Wrote: > > > On Wed, 23 Jun 2010 00:41:45 -0700, Ali Çehreli wrote: > > > > > Ali Çehreli wrote: > > >> dcoder wrote: > > >> > > >> > So, I moved the initialization to inside the main function, and now > > >> it works. > > >> > Great. I think we need

Re: Cannot initialize associative array.

2010-06-24 Thread Andrej Mitrovic
Bernard Helyer Wrote: > On Wed, 23 Jun 2010 00:41:45 -0700, Ali Çehreli wrote: > > > Ali Çehreli wrote: > >> dcoder wrote: > >> > >> > So, I moved the initialization to inside the main function, and now > >> it works. > >> > Great. I think we need to put this question in the FAQ. > >> > >>

Re: Cannot initialize associative array.

2010-06-23 Thread Rory McGuire
On Wed, 23 Jun 2010 00:30:40 +0200, Ali Çehreli wrote: dcoder wrote: > So, I moved the initialization to inside the main function, and now it works. > Great. I think we need to put this question in the FAQ. For future reference, if it really needs to be global: uint[string] mywords; s

Re: Cannot initialize associative array.

2010-06-23 Thread Bernard Helyer
On Wed, 23 Jun 2010 00:41:45 -0700, Ali Çehreli wrote: > Ali Çehreli wrote: >> dcoder wrote: >> >> > So, I moved the initialization to inside the main function, and now >> it works. >> > Great. I think we need to put this question in the FAQ. >> >> For future reference, if it really needs to

Re: Cannot initialize associative array.

2010-06-23 Thread bearophile
Ali Çehreli: > Could someone please verify whether the above is really necessary? An initialization inside some runtime function/initializator is necessary unless the AA is an enum. > Is it > actually a dmd bug that we need to use 'static this()' to initialize an > associative array? Accord

Re: Cannot initialize associative array.

2010-06-23 Thread Pelle
On 06/23/2010 09:41 AM, Ali Çehreli wrote: Ali Çehreli wrote: dcoder wrote: > So, I moved the initialization to inside the main function, and now it works. > Great. I think we need to put this question in the FAQ. For future reference, if it really needs to be global: uint[string] mywords; s

Re: Cannot initialize associative array.

2010-06-23 Thread Ali Çehreli
Ali Çehreli wrote: dcoder wrote: > So, I moved the initialization to inside the main function, and now it works. > Great. I think we need to put this question in the FAQ. For future reference, if it really needs to be global: uint[string] mywords; static this() { mywords = [ "Hello"

Re: Cannot initialize associative array.

2010-06-22 Thread Ali Çehreli
dcoder wrote: > So, I moved the initialization to inside the main function, and now it works. > Great. I think we need to put this question in the FAQ. For future reference, if it really needs to be global: uint[string] mywords; static this() { mywords = [ "Hello" : 1, "World" : 1, "Cat

Re: Cannot initialize associative array.

2010-06-22 Thread dcoder
> > For the declaration: > > uint[string] mywords = [ "Hello" : 1, "World" : 1, > > "Cat" : 1, "Dog" : 1 ]; > > > > > > I get: > > > > $ dmd test_01.d > > test_01.d(3): Error: non-constant expression > > ["Hello":1u,"World":1u,"Cat":1u,"Dog":1u] > AAs can't be assigned to at compile time (:[). You

Re: Cannot initialize associative array.

2010-06-22 Thread bearophile
Bernard Helyer: > AAs can't be assigned to at compile time (:[). You can define enum ones, this works: import std.stdio; enum int[string] aa = ["foo": 10]; void main() { writeln(cast(bool)("foo" in aa)); writeln(aa["foo"]); writeln(cast(bool)("hello" in aa)); } But this code: impor

Re: Cannot initialize associative array.

2010-06-22 Thread Bernard Helyer
On Tue, 22 Jun 2010 21:32:48 +, dcoder wrote: > Sorry, I forgot to put some compiler output: > > For the declaration: uint[string] mywords = [ "Hello" : 1, "World" : 1, > "Cat" : 1, "Dog" : 1 ]; > > > I get: > > $ dmd test_01.d > test_01.d(3): Error: non-constant expression > ["Hello":1u,"

Re: Cannot initialize associative array.

2010-06-22 Thread dcoder
Sorry, I forgot to put some compiler output: For the declaration: uint[string] mywords = [ "Hello" : 1, "World" : 1, "Cat" : 1, "Dog" : 1 ]; I get: $ dmd test_01.d test_01.d(3): Error: non-constant expression ["Hello":1u,"World":1u,"Cat":1u,"Dog":1u]

Cannot initialize associative array.

2010-06-22 Thread dcoder
Hello. I have the following d code which fails to compile. All 3 initializations of mywords fail. What am I doing wrong? thanks. $ dmd --help Digital Mars D Compiler v2.042 Copyright (c) 1999-2010 by Digital Mars written by Walter Bright Documentation: http://www.digitalmars.com/d/2.0/index.h