// Assume bar is some associative array of type Foo[string]
Foo* value = key in bar;
if (!value) {
bar[key] = Foo.init;
value = &bar[key];
}
This seems sub-optimal, given that in involves three hashes (two lookups
and one insertion). Is there a more efficient or cleaner way to do so?
S
Rikki Cattermole:
Foo*[string] bar;
Foo v = *bar.grab("mykey");
Is this the setdefault of Python dicts? If this need is strong a
new function could be added to Phobos (or even druntime if you
want to reduce the number of hash computations).
Bye,
bearophile
On 18/02/2015 1:21 p.m., Matt Kline wrote:
In C++, the index operator for maps will either return a reference to
the existing value if the key can be found, or a reference to a new,
default-initialized value if one with the given key cannot be found.
In D, an exception is thrown instead when a v
On Wednesday, 18 February 2015 at 00:21:11 UTC, Matt Kline wrote:
if (value) {
should of course be
if (!value) {
Sorry for the typo.