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" : RoundingMode.towardZero,
     "=0": RoundingMode.tiesToEven,
     "=^": RoundingMode.tiesToAway
];


Error: non-constant expression `[">":cast(RoundingMode)2, 
"<":cast(RoundingMode)3, "0":cast(RoundingMode)4, 
"=0":cast(RoundingMode)0, "=^":cast(RoundingMode)1]`


RoundingMode is an enum.


An alternative to the shared module constructor is to declare it as an 
enum instead of immutable. But that will allocate a new associative 
array every time the enum is referenced.


enum a = [1: 2, 3: 4];

--
/Jacob Carlborg


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 really isn't anything special about whatever module you have main in.
It's just like any other module except that it has main in it. Technically,
you could put main somewhere deep in your module hierarchy. It's just
probably not the best idea from an organizational standpoint.

- Jonathan M Davis



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, ironically, only if the 
variable is not `static`...


I believe this is technically an implementation shortcoming - 
it is supposed to work in a static context too, but it isn't 
implemented. But regardless, right now, you need to do it in a 
function (or a static constructor) right now.


You can separate declaration from initialization on 
module-level like so:


immutable RoundingMode[string] ibmRounding;
shared static this() {
  ibmRounding =
  [
 ">" : RoundingMode.towardPositive,
 "<" : RoundingMode.towardNegative,
 "0" : RoundingMode.towardZero,
 "=0": RoundingMode.tiesToEven,
 "=^": RoundingMode.tiesToAway
  ];
}


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.




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 technically an implementation shortcoming - it 
is supposed to work in a static context too, but it isn't 
implemented. But regardless, right now, you need to do it in a 
function (or a static constructor) right now.


You can separate declaration from initialization on module-level 
like so:


immutable RoundingMode[string] ibmRounding;
shared static this() {
  ibmRounding =
  [
 ">" : RoundingMode.towardPositive,
 "<" : RoundingMode.towardNegative,
 "0" : RoundingMode.towardZero,
 "=0": RoundingMode.tiesToEven,
 "=^": RoundingMode.tiesToAway
  ];
}


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": RoundingMode.tiesToEven,
"=^": RoundingMode.tiesToAway
];


Error: non-constant expression `[">":cast(RoundingMode)2, 
"<":cast(RoundingMode)3, "0":cast(RoundingMode)4, 
"=0":cast(RoundingMode)0, "=^":cast(RoundingMode)1]`			


RoundingMode is an enum.


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 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" : 1, "Dog" : 1 ];
> > >> }
> > > 
> > > Could someone please verify whether the above is really necessary? Is it
> > > actually a dmd bug that we need to use 'static this()' to initialize an
> > > associative array?
> > > 
> > > Ali
> > 
> > I can't remember where exactly I read it, but there's a line in the docs 
> > specifically forbidding the use of AAs in constant expressions, so it's 
> > by design AFAIK.
> 
> http://www.digitalmars.com/d/2.0/expression.html#ArrayLiteral
> 
> "Associative array literals are a comma-separated list of key:value pairs...
> *An AssocArrayLiteral cannot be used to statically initialize anything. *"

Woops, correct link:
http://www.digitalmars.com/d/2.0/expression.html#AssocArrayLiteral


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.
> >> 
> >> For future reference, if it really needs to be global:
> >> 
> >> uint[string] mywords;
> >> 
> >> static this()
> >> {
> >> mywords = [ "Hello" : 1, "World" : 1, "Cat" : 1, "Dog" : 1 ];
> >> }
> > 
> > Could someone please verify whether the above is really necessary? Is it
> > actually a dmd bug that we need to use 'static this()' to initialize an
> > associative array?
> > 
> > Ali
> 
> I can't remember where exactly I read it, but there's a line in the docs 
> specifically forbidding the use of AAs in constant expressions, so it's 
> by design AFAIK.

http://www.digitalmars.com/d/2.0/expression.html#ArrayLiteral

"Associative array literals are a comma-separated list of key:value pairs...
*An AssocArrayLiteral cannot be used to statically initialize anything. *"


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;

static this()
{
 mywords = [ "Hello" : 1, "World" : 1, "Cat" : 1, "Dog" : 1 ];
}

Ali




from what I have read in TDPL so far (about half way now), there is no  
mention of this limitation.

It just says that [key:value] is how you would statically initialize a AA.

Does this mean that it is just a D implementation issue. To me its seems  
like this should be a defined part
of the language. Also the compiler should really rewrite it to your above  
code anyway, surely?


-Rory


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 be global:
>> 
>> uint[string] mywords;
>> 
>> static this()
>> {
>> mywords = [ "Hello" : 1, "World" : 1, "Cat" : 1, "Dog" : 1 ];
>> }
> 
> Could someone please verify whether the above is really necessary? Is it
> actually a dmd bug that we need to use 'static this()' to initialize an
> associative array?
> 
> Ali

I can't remember where exactly I read it, but there's a line in the docs 
specifically forbidding the use of AAs in constant expressions, so it's 
by design AFAIK.


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?

According to the way D AAs are designed it's not a bug. But of course you can 
think of possible enhancements or changes to the design.

Bye,
bearophile


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;

static this()
{
mywords = [ "Hello" : 1, "World" : 1, "Cat" : 1, "Dog" : 1 ];
}


Could someone please verify whether the above is really necessary? Is it
actually a dmd bug that we need to use 'static this()' to initialize an
associative array?

Ali


I say it's a bug, a literal like that should be a constant expression. 
Someone report it!


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" : 1, "World" : 1, "Cat" : 1, "Dog" : 1 ];
}


Could someone please verify whether the above is really necessary? Is it 
actually a dmd bug that we need to use 'static this()' to initialize an 
associative array?


Ali


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" : 1, "Dog" : 1 ];
}

Ali


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'll have to use a
> static constructor or an initiliasation function to assign to one at
> global scope.

thanks Bernard.

I see, so for global associative arrays, I can't do it.  That's good to know, 
and
I guess that will force me into better coding habits.  Anyways, I just wish the
compiler could give me a better message than what I got.  :(

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.

Many thanks.



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:

import std.stdio;
immutable int[string] aa = ["foo": 10];
void main() {
writeln(cast(bool)("foo" in aa));
writeln(aa["foo"]);
writeln(cast(bool)("hello" in aa));
}

Raises the compilation error:
test.d(2): Error: non-constant expression ["foo":10]
In theory aa here is a constant expression :-)

Bye,
bearophile


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,"World":1u,"Cat":1u,"Dog":1u]

AAs can't be assigned to at compile time (:[). You'll have to use a 
static constructor or an initiliasation function to assign to one at 
global scope.



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.html
Usage:


import std.algorithm, std.stdio, std.string;

//uint[string] mywords = [ "Hello" : 1, "World" : 1, "Cat" : 1, "Dog" : 1 ];
//auto mywords = [ "Hello" : 1, "World" : 1, "Cat" : 1, "Dog" : 1 ];
const uint[const string] mywords = [ "Hello" : 1, "World" : 1, "Cat" : 1,
"Dog" : 1 ];

void main() {
  foreach( line; stdin.byLine()) {
foreach( word; split(strip(line))) {
  string s = word.idup;
  if( s in mywords)
writefln( "%s is a word I know.", s);
  else
writefln( "%s is not a word I know.", s);
}
  }

  return;
}