Associative array literal: length wrong when duplicate keys found
Ivan Kazmenko via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jan 31 06:15:58 PST 2017
Hi.
I wanted to check whether a few variables of the same type are
all distinct, in a quick and dirty way. I tried to do it similar
to Python's "len(set(value_list)) == len(value_list)" idiom by
using an associative array (AA). At this point, I found out that
when initializing the AA with a literal, the length is the number
of keys given, regardless of whether some of them were the same.
A minimized example:
-----
import std.stdio;
void main () {
auto aa = [1 : 2, 1 : 3];
writeln (aa.length, " ", aa); // 2 [1:3, ]
}
-----
See, the length is 2, but iteration over aa yields only one
key:value pair. Also, note the comma which is a sign of internal
confusion as well.
My question is, what's the state of this? Is this a bug? Or
should it be forbidden to have such an initializer? Or maybe it
is a feature with some actual merit?
Ivan Kazmenko.
More information about the Digitalmars-d-learn
mailing list