www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Initialization of the Associative arrays

reply baleog <maccarka yahoo.com> writes:
Hello,

Is it a bug that i can't do like this:
int[string] x = ["11":11, "1":1];
Error: cannot implicitly convert expression ("1") of type invariant(char[1u])
to invariant(char[2u])

dmd-2.0.16

Thanks
Jul 14 2008
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"baleog" wrote
 Hello,

 Is it a bug that i can't do like this:
 int[string] x = ["11":11, "1":1];
 Error: cannot implicitly convert expression ("1") of type 
 invariant(char[1u]) to invariant(char[2u])
It's a foolish issue with D and static arrays. (seriously, I must see about 1 post a week asking why static arrays don't work right, shouldn't this be a priority?) Try this workaround: int[string] x = ["11"[]:11, "1":1]; -Steve
Jul 14 2008
prev sibling next sibling parent reply Don <nospam nospam.com.au> writes:
baleog wrote:
 Hello,
 
 Is it a bug that i can't do like this:
 int[string] x = ["11":11, "1":1];
 Error: cannot implicitly convert expression ("1") of type invariant(char[1u])
to invariant(char[2u])
 
 dmd-2.0.16
 
 Thanks
No. You need to tell the compiler that you're using dynamic, not static arrays. Just add [] after the first entry. int[string] x = ["11"[]:11, "1":1];
Jul 14 2008
parent reply "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"Don" <nospam nospam.com.au> wrote in message 
news:g5fmrg$e7o$1 digitalmars.com...
 baleog wrote:
 Hello,

 Is it a bug that i can't do like this:
 int[string] x = ["11":11, "1":1];
<snip>
 No. You need to tell the compiler that you're using dynamic, not static 
 arrays. Just add [] after the first entry.

 int[string] x = ["11"[]:11, "1":1];
If it isn't a bug, it's certainly a design silliness: - that array literals take their type from the first element, rather than the lowest common denominator - that there aren't static initialisers for AAs as there are for LAs Stewart. -- My e-mail address is valid but not my primary mailbox. Please keep replies on the 'group where everybody may benefit.
Jul 14 2008
next sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
Stewart Gordon a écrit :
 "Don" <nospam nospam.com.au> wrote in message 
 news:g5fmrg$e7o$1 digitalmars.com...
 baleog wrote:
 Hello,

 Is it a bug that i can't do like this:
 int[string] x = ["11":11, "1":1];
<snip>
 No. You need to tell the compiler that you're using dynamic, not 
 static arrays. Just add [] after the first entry.

 int[string] x = ["11"[]:11, "1":1];
If it isn't a bug, it's certainly a design silliness: - that array literals take their type from the first element, rather than the lowest common denominator - that there aren't static initialisers for AAs as there are for LAs Stewart.
What use has a static array in comparison to a dynamic array?
Jul 14 2008
parent reply "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"Ary Borenszweig" <ary esperanto.org.ar> wrote in message 
news:g5gpac$2hhf$1 digitalmars.com...
<snip>
 What use has a static array in comparison to a dynamic array?
I'm not sure what you're talking about. Stewart. -- My e-mail address is valid but not my primary mailbox. Please keep replies on the 'group where everybody may benefit.
Jul 14 2008
parent Ary Borenszweig <ary esperanto.org.ar> writes:
Stewart Gordon a écrit :
 "Ary Borenszweig" <ary esperanto.org.ar> wrote in message 
 news:g5gpac$2hhf$1 digitalmars.com...
 <snip>
 What use has a static array in comparison to a dynamic array?
I'm not sure what you're talking about. Stewart.
What I mean is which advantages gives you a static array that a dynamic array can't? The language seems full of cases where static arrays give problems. The only difference I can see is that they have a fixed length at compile time, and maybe that fact can be used for some optimizations. But does that optimization worths it to give the language such problems?
Jul 15 2008
prev sibling next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Stewart Gordon wrote:
 "Don" <nospam nospam.com.au> wrote in message 
 news:g5fmrg$e7o$1 digitalmars.com...
 baleog wrote:
 Hello,

 Is it a bug that i can't do like this:
 int[string] x = ["11":11, "1":1];
<snip>
 No. You need to tell the compiler that you're using dynamic, not 
 static arrays. Just add [] after the first entry.

 int[string] x = ["11"[]:11, "1":1];
If it isn't a bug, it's certainly a design silliness: - that array literals take their type from the first element, rather than the lowest common denominator
Amen. [1,2,3.0,4] should be treated as an array of doubles.
 - that there aren't static initialisers for AAs as there are for LAs
Yeh, that sucks too. Although I can see it might be tricky to get working. Also sucks that initializers for structs only work for static structs. That one doesn't seem like it should be so hard to fix, but there must be some catch that prevented W. from throwing it in. --bb
Jul 14 2008
prev sibling parent reply BCS <ao pathlink.com> writes:
Reply to Stewart,

 "Don" <nospam nospam.com.au> wrote in message
 news:g5fmrg$e7o$1 digitalmars.com...
 
 baleog wrote:
 
 Hello,
 
 Is it a bug that i can't do like this:
 int[string] x = ["11":11, "1":1];
<snip>
 No. You need to tell the compiler that you're using dynamic, not
 static arrays. Just add [] after the first entry.
 
 int[string] x = ["11"[]:11, "1":1];
 
If it isn't a bug, it's certainly a design silliness: - that array literals take their type from the first element, rather than the lowest common denominator - that there aren't static initialisers for AAs as there are for LAs Stewart.
I think much of this might come from the need to be able to eyeball parse the code auto a = [ .... 16 different function calls ...];// what is the type of a? I think as long as the type of an array literal isn't from the type of the assigned the current solution is about as good as it will get.
Jul 14 2008
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
BCS wrote:
 Reply to Stewart,
 
 "Don" <nospam nospam.com.au> wrote in message
 news:g5fmrg$e7o$1 digitalmars.com...

 baleog wrote:

 Hello,

 Is it a bug that i can't do like this:
 int[string] x = ["11":11, "1":1];
<snip>
 No. You need to tell the compiler that you're using dynamic, not
 static arrays. Just add [] after the first entry.

 int[string] x = ["11"[]:11, "1":1];
If it isn't a bug, it's certainly a design silliness: - that array literals take their type from the first element, rather than the lowest common denominator - that there aren't static initialisers for AAs as there are for LAs Stewart.
I think much of this might come from the need to be able to eyeball parse the code auto a = [ .... 16 different function calls ...];// what is the type of a?
Who cares what the type is as long as it's sufficient to hold the results of all those function calls? If you cared you wouldn't have been using auto there.
 I think as long as the type of an array literal isn't from the type of 
 the assigned the current solution is about as good as it will get.
Polysemous vals may help here if they ever materialize. Its not a huge issue in the grand scheme of things, but still feels slightly sub-optimal currently. NumPy arrays use the rule "array type is the minimal type that can hold all elements" and it seems to work out very well in practice. Seems to be the path of least surprise for this problem. --bb
Jul 14 2008
parent BCS <ao pathlink.com> writes:
Reply to Bill,

 I think much of this might come from the need to be able to eyeball
 parse the code
 
 auto a = [ .... 16 different function calls ...];// what is the type
 of a?
 
Who cares what the type is as long as it's sufficient to hold the results of all those function calls? If you cared you wouldn't have been using auto there.
"Who cares" is correct. Maybe not the guy who wrote it, but 6 months down the line it might crop up and make a big difference to someone else ("what overload is this using?", "Why can't I cast to X", "at what point will this overflow", etc.)
Jul 15 2008
prev sibling parent "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"BCS" <ao pathlink.com> wrote in message 
news:55391cb32f3218cab3ecfa6cf778 news.digitalmars.com...
<snip>
 I think much of this might come from the need to be able to eyeball parse 
 the code

 auto a = [ .... 16 different function calls ...];// what is the type of a?
But we're not dealing with an auto here. We're dealing with an int[string]. With autotype declarations, it's necessary to treat initialisers as expressions. When the type is specified, arrays can use static initialisers.
 I think as long as the type of an array literal isn't from the type of the 
 assigned the current solution is about as good as it will get.
No, as long as the compiler treats the original attempt as an array literal rather than as a static initialiser. Stewart. -- My e-mail address is valid but not my primary mailbox. Please keep replies on the 'group where everybody may benefit.
Jul 14 2008
prev sibling parent Moritz Warning <moritzwarning web.de> writes:
On Mon, 14 Jul 2008 10:07:15 -0400, baleog wrote:

 Hello,
 
 Is it a bug that i can't do like this: int[string] x = ["11":11, "1":1];
 Error: cannot implicitly convert expression ("1") of type
 invariant(char[1u]) to invariant(char[2u])
 
 dmd-2.0.16
 
 Thanks
DMD deduce the type of the literal from it's first element. int[string] x = [cast(char[]) "11":11, "1":1]; would work. "11"[] is a shortcut. Imho, the left side type declaration should be honored by the compiler by adding a cast to the first literal element after initial parsing.
Jul 14 2008