www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error with associative array initializer

reply Peter Neubauer <admiral.nything gmail.com> writes:
I'm trying to initialize an associative array with key int and value type
char[] like this:

char[][int] stuff = [
	0: "abc",
	1: "def",
	2: "ghi"
];

... which works just fine as long as the strings are all of equal length. For
the following code:

char[][int] stuff2 = [
	0: "ab",
	1: "def",
	2: "ghi"
];

results in the error message "cannot implicitly convert expression ("def") of
type char[3] to char[2]". Just the same thing happens if the first value in the
array is of greater length than the others. It all depends on the first string.
How can I avoid this?
Jun 30 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Peter Neubauer wrote:
 I'm trying to initialize an associative array with key int and value type
char[] like this:
 
 char[][int] stuff = [
 	0: "abc",
 	1: "def",
 	2: "ghi"
 ];
 
 ... which works just fine as long as the strings are all of equal length. For
the following code:
 
 char[][int] stuff2 = [
 	0: "ab",
 	1: "def",
 	2: "ghi"
 ];
 
 results in the error message "cannot implicitly convert expression ("def") of
type char[3] to char[2]". Just the same thing happens if the first value in the
array is of greater length than the others. It all depends on the first string.
How can I avoid this?
Make sure the type of the first expression is a dynamic array. "ab"[] or cast(char[])"ab" would work.
Jun 30 2007
next sibling parent Peter Neubauer <admiral.nything gmail.com> writes:
Thanks, that fixed it for me!

char[][int] stuff2 = [
	0: "abc"[],
	1: "de",
	2: "anylengthstring"
];

Frits van Bommel Wrote:
 Make sure the type of the first expression is a dynamic array. "ab"[] or 
 cast(char[])"ab" would work.
Jun 30 2007
prev sibling parent reply Pontus <pontus update.uu.se> writes:
 
 Make sure the type of the first expression is a dynamic array. "ab"[] or
 cast(char[])"ab" would work.
I see that it works, but is it not a bit weird? The type declarations is char[][int], the first part(char[]) looks like a dynamic array to me. Why would the compiler assume it is char[2] just because the first element of the intializer looks like it? /P
Jul 03 2007
next sibling parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Pontus wrote:
 Make sure the type of the first expression is a dynamic array. "ab"[] or
 cast(char[])"ab" would work.
I see that it works, but is it not a bit weird? The type declarations is char[][int], the first part(char[]) looks like a dynamic array to me. Why would the compiler assume it is char[2] just because the first element of the intializer looks like it?
It probably doesn't even look at the type of the declaration again until it has processed the initializer expression. I'm actually a bit surprised that assigning a char[3][int] literal to a char[][int] works at all. The compiler probably has some special handling for those kinds of cases when it compares the types of the declaration and that of the initializer expression. I guess given that fact it wouldn't be too unreasonable to expect the original code to work as well. It would certainly be nice if it would just do the Right Thing(TM).
Jul 03 2007
prev sibling parent Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
Pontus skrev:
 Make sure the type of the first expression is a dynamic array. "ab"[] or
 cast(char[])"ab" would work.
I see that it works, but is it not a bit weird? The type declarations is char[][int], the first part(char[]) looks like a dynamic array to me. Why would the compiler assume it is char[2] just because the first element of the intializer looks like it?
Walter thought the alternative behavior would be confusing to users. From all users asking the the same question, I'd say it seems to be the other way around. /Oskar
Jul 03 2007