dcoder
Joined: 04 May 2010 Posts: 5
|
Posted: Thu Jun 10, 2010 11:26 am Post subject: How to initialize associative arrays? |
|
|
Hello. Still going through the tutorials.
The following program does not work:
Code: |
import std.stdio;
import std.string;
static string greetings[string] =
[ "Klaus":"Guten Tag", "Dave":"Hello",
"Maria":"ibuenas tardes", "Antoine":"Bon Jour" ];
void main() {
foreach( string name, string msg; greetings) {
writefln( "%s: %s!", name, msg);
}
return;
}
|
Here's the error:
$ dmd Greetings.d
Greetings.d(5): Error: non-constant expression ["Klaus":"Guten Tag","Dave":"Hello","Maria":"ibuenas tardes","Antoine":"Bon Jour"]
Well, I don't understand why the compiler considers the initialization list non-constant. What is varying?
So poking around the internet, it seems like I need to declare my greetings assoc array as static, but that didn't work.
And, then perhaps I need to throw some const in there:
Code: |
import std.stdio;
import std.string;
const string greetings[const string] =
[ "Klaus":"Guten Tag", "Dave":"Hello",
"Maria":"ibuenas tardes", "Antoine":"Bon Jour" ];
void main() {
foreach( const string name, const string msg; greetings) {
writefln( "%s: %s!", name, msg);
}
return;
}
|
$ dmd Greetings.d
Greetings.d(5): Error: non-constant expression ["Klaus":"Guten Tag","Dave":"Hello","Maria":"ibuenas tardes","Antoine":"Bon Jour"]
Internal error: e2ir.c 4600
Aside from my ignorance, what is going on here? How can I fix this?
thanks. |
|