View mode: basic / threaded / horizontal-split · Log in · Help
June 30, 2007
Error with associative array initializer
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?
June 30, 2007
Re: Error with associative array initializer
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.
June 30, 2007
Re: Error with associative array initializer
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.
July 03, 2007
Re: Error with associative array initializer
> 
> 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
July 03, 2007
Re: Error with associative array initializer
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).
July 03, 2007
Re: Error with associative array initializer
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
Top | Discussion index | About this forum | D home