Associative value order

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 6 11:50:43 PDT 2014


On Wed, Aug 06, 2014 at 03:33:15PM -0300, Ary Borenszweig via Digitalmars-d-learn wrote:
[...]
> Why is a dictionary something built-in the language?

It's actually one of the things I really liked about D when I was first
considering it.

I hated the fact that it took until C++11 to even *get* a proper hash
table into the C++ standard, and even now, it's a royal pain in the neck
to even use them (e.g., you have to define your own hash function if you
want to use a struct as key), because the language just wasn't designed
to support them in a non-verbose, non-contorted way. In D, it Just
Works(tm). (Well, excepting the numerous AA-related bugs in D, but
that's more of a quality-of-implementation issue. The idea itself is
sound, IMO.)


> Can't it be some standard library class/struct with syntax sugar for
> creation?

That's where we hope to get to eventually. But we're not quite there
yet.


> All of these questions about associative arrays wouldn't exist if the
> source code for these operations was available.
> 
> (it's probably available, but buried in some C++ code, I guess, on in
> dmd?)

It's certainly available, and it's in D, not C++:

	https://github.com/D-Programming-Language/druntime/blob/master/src/rt/aaA.d

But that's beside the point. The point is that the AA interface does not
specify the ordering of keys/values, precisely because we want to have
the flexibility of changing the implementation (e.g., if we decide to
implement a superior hash collision resolution algorithm, replace the
default hash function, etc.) without affecting user code. User code that
depends on the specifics of the current implementation are, strictly
speaking, broken, because they break the encapsulation of AA's.

People simply need to learn that AA's are *unordered*. This is D, not
PHP. If they want an ordered dictionary, they really should be using
something else instead, perhaps std.container.RedBlackTree.


T

-- 
Windows: the ultimate triumph of marketing over technology. -- Adrian von Bidder


More information about the Digitalmars-d-learn mailing list