On Tuesday, 17 April 2018 at 20:49:30 UTC, Steven Schveighoffer
wrote:
Why do you think it's less efficient to use a lazy parameter?
Wouldn't an extra function call have to happen, at least in some
cases?
This pattern needs a pointer to be returned, instead of using
`ref`. Note that `&inserted` is valid in @safe code, but only
with -dip1000. I called the function `slot` because it always
returns the address of the slot which the value is stored in.
Returning ref makes more sense to me -- you are never going to
return null.
How do you implement this if the function returns with ref:
bool inserted;
auto p = aa.slot("key", &inserted);
if (inserted) {
...
// set *p
}
else {
// read *p
...
// set *p
}
There is a common basic use case for this - counting the
occurrence of a key in a data set. If the key doesn't exist,
initialize and insert the value. *Iff* it does exist, increment
the value - I don't think you can do this without functional
contortions with your ref return.
(A side benefit is that returning a pointer is consistent with
`in`.)