Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
November 05, 2015 Associative array with duplicated keys? | ||||
---|---|---|---|---|
| ||||
Check this: http://dpaste.dzfl.pl/ebbb3ebac60e It doesn't give any error or warning. And writeln seems confused (do you see that "," at the end?) |
November 05, 2015 Re: Associative array with duplicated keys? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On Thursday, 5 November 2015 at 08:55:10 UTC, Andrea Fontana wrote:
> Check this:
> http://dpaste.dzfl.pl/ebbb3ebac60e
>
> It doesn't give any error or warning. And writeln seems confused (do you see that "," at the end?)
I am sure the coder of writeln was lazy to prevent putting ", " after last item. You can report it as a bug I guess.
|
November 05, 2015 Re: Associative array with duplicated keys? | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | On Thursday, 5 November 2015 at 09:27:35 UTC, tcak wrote:
> On Thursday, 5 November 2015 at 08:55:10 UTC, Andrea Fontana wrote:
>> Check this:
>> http://dpaste.dzfl.pl/ebbb3ebac60e
>>
>> It doesn't give any error or warning. And writeln seems confused (do you see that "," at the end?)
>
> I am sure the coder of writeln was lazy to prevent putting ", " after last item. You can report it as a bug I guess.
Not really: it appears only if array has duplicated keys.
Anyway: are duplicated keys on declaration allowed?
|
November 05, 2015 Re: Associative array with duplicated keys? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On Thursday, 5 November 2015 at 10:04:02 UTC, Andrea Fontana wrote:
> Anyway: are duplicated keys on declaration allowed?
IMHO This should at least be a warning.
|
November 05, 2015 Re: Associative array with duplicated keys? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On Thursday, 5 November 2015 at 08:55:10 UTC, Andrea Fontana wrote:
> Check this:
> http://dpaste.dzfl.pl/ebbb3ebac60e
>
> It doesn't give any error or warning. And writeln seems confused (do you see that "," at the end?)
This is an outright bug, please report on issues.dlang.org:
void main()
{
import std.stdio : writeln;
["key": 10, "key" : 20, "key" : 30].length.writeln;
["key" : 30].length.writeln;
}
Prints:
3
1
|
November 05, 2015 Re: Associative array with duplicated keys? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | https://issues.dlang.org/show_bug.cgi?id=15290 |
November 05, 2015 Re: Associative array with duplicated keys? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On Thursday, 5 November 2015 at 10:04:02 UTC, Andrea Fontana wrote:
> Anyway: are duplicated keys on declaration allowed?
They shouldn't be...
|
November 06, 2015 Re: Associative array with duplicated keys? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Thursday, 5 November 2015 at 13:08:20 UTC, Adam D. Ruppe wrote:
> On Thursday, 5 November 2015 at 10:04:02 UTC, Andrea Fontana wrote:
>> Anyway: are duplicated keys on declaration allowed?
>
> They shouldn't be...
Why? I'll admit it is something I've never even thought of using, but every language I know (just tested with python, lua, scheme and js) accepts it and as far as I can tell from a user perspective boils
auto aa = ["a":10, "b", 42, "a":20];
down to:
int[string] aa;
aa["a"] = 10;
aa["b"] = 42;
aa["a"] = 20;
This is deterministic and could allow one to build an associative array through a mixin for example while still giving default values.
As I said, it's not something I've used but I think we should consider it.
|
November 06, 2015 Re: Associative array with duplicated keys? | ||||
---|---|---|---|---|
| ||||
Posted in reply to cym13 | On Friday, 6 November 2015 at 14:28:53 UTC, cym13 wrote:
> auto aa = ["a":10, "b", 42, "a":20];
This should read auto aa = ["a":10, "b":42, "a":20]; of course.
|
Copyright © 1999-2021 by the D Language Foundation