[Issue 9491] [AA] allow for insert/return pointer to existing value if present on assoc array

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Apr 9 00:54:06 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=9491

w0rp <devw0rp at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |devw0rp at gmail.com

--- Comment #1 from w0rp <devw0rp at gmail.com> ---
I know exactly the kind of thing you are talking about. What you want is a
setDefault function with an overload. These functions should have these
signatures.

ref V1 setDefault(K, V1, V2)(ref V1[K] map, K key, lazy V2 value) if (is(V2 :
V1));
ref V setDefault(K, V)(ref V[K] map, K key);

Where the second overload is equivalent to setDefault(map, key, V1.init) above.
This is basically 'setdefault' from Python, only better because the value is
lazy.

I have written a higher level implementation of this myself before, but I
believe a druntime implementation could do better by computing a hash for K
once, like you say.

--


More information about the Digitalmars-d-bugs mailing list