digitalmars.D.learn - Error with associative array initializer
- Peter Neubauer (13/13) Jun 30 2007 I'm trying to initialize an associative array with key int and value typ...
- Frits van Bommel (3/20) Jun 30 2007 Make sure the type of the first expression is a dynamic array. "ab"[] or...
- Peter Neubauer (7/9) Jun 30 2007 Thanks, that fixed it for me!
- Pontus (5/8) Jul 03 2007 I see that it works, but is it not a bit weird?
- Frits van Bommel (10/18) Jul 03 2007 It probably doesn't even look at the type of the declaration again until...
- Oskar Linde (5/13) Jul 03 2007 Walter thought the alternative behavior would be confusing to users.
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
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
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
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
Pontus wrote: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).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?
Jul 03 2007
Pontus skrev: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. /OskarMake 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?
Jul 03 2007