Re: Mimicking C++'s indexing behavior in D associative arrays

2015-02-18 Thread bearophile via Digitalmars-d-learn
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

Re: Mimicking C++'s indexing behavior in D associative arrays

2015-02-18 Thread FG via Digitalmars-d-learn
// 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?

Mimicking C++'s indexing behavior in D associative arrays

2015-02-17 Thread Matt Kline via Digitalmars-d-learn
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 value with the given key cannot be

Re: Mimicking C++'s indexing behavior in D associative arrays

2015-02-17 Thread Matt Kline via Digitalmars-d-learn
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.