foreach ref very broken: fails to call front(val)
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Tue Jul 10 07:19:05 PDT 2012
On 7/10/12 4:29 AM, kenji hara wrote:
> Posted a pull request:
> https://github.com/D-Programming-Language/phobos/pull/678
Hmm, that has a couple of issues.
First, map that modifies things in place is a bug more often than a
feature given that the caller of map may use front() an arbitrary number
of times. So I see little intentional and legitimate use for things like
map!"a += 2"(range).
Second, the return by value from Array is intentional and has to do with
sealing. Array is intended to never escape the addresses of its
elements. That way, the collection is "sealed" in the sense there can
never be uncontrolled pointers to its elements. This allows using
efficient allocation strategies for the array without compromising its
safety.
In more recent discussions with Walter we discussed the possibility of
making "ref" returns transitory, i.e. no caller code can actually save a
ref result beyond the call. That would allow us to keep sealing while
also benefitting of ref returns. But that feature has not been
implemented yet.
Andrei
More information about the Digitalmars-d
mailing list