Feature to get or add value to an associative array.
Steven Schveighoffer
schveiguy at yahoo.com
Tue Apr 17 14:24:58 UTC 2018
On 4/15/18 6:52 PM, Giles Bathgate wrote:
> I find this later code clunky and it requires two hashes/lookups. The
> proposed implementation adds support directly to rt/aaA.d to avoid this.
> I should also point out that the allocation of a new Person in the
> example is trivial, but it might often be the case that the person
> instance is created by fetching a record from a db, or an Image loaded
> from disk, or a movie downloaded from the internet.
I think this is a great addition. I've always disliked the double-lookup
requirement for ensuring a key was initialized. I'll note that C++ gets
around this by always initializing on any access.
The name "getOrAdd" is a bit mechanical sounding. The real reason you
want this is to ensure that key's value is initialized.
ensureInitialized is long. get is already taken (if get weren't already
taken, I'd suggest that name). Others have suggested using flags, but
I'll note to them, `Flag` and `Yes` are part of std.typecons, and not
accessible here.
getWithDefault sounds reasonable but probably would be confusing with
the current `get` overload. Maybe getInitialized? I don't know that
either of these are giant leaps ahead of getOrAdd.
Another possibility is getPtr. get currently gets a value, or returns a
default value if it doesn't exist. But getPtr would return a pointer to
the value, ensuring it's initialized.
In any case, thumbs up to the concept, and I'm ambivalent on the name.
-Steve
More information about the Digitalmars-d
mailing list