On Sunday, 23 March 2014 at 11:52:53 UTC, Timon Gehr wrote:
On 03/23/2014 01:50 AM, Walter Bright wrote:

2. Can r.front be called n times in a row? I.e. is calling front()
destructive?

If true, this means that r.front will have to cache a copy in many cases.

An analogous problem exists and is more severe for RandomAccessRanges.

import std.algorithm, std.range;
class Obj{ this(int){} }
void main(){
    auto x=[1,2,3].map!(a=>new Obj(a));
    auto a=x.front;
    auto b=x.front;
    auto c=x[0];
    auto d=x[0];
    assert(a is b); // fail
    assert(b is c); // fail
    assert(c is d); // fail
}

I have an open proposal for a "cache" range adaptor that somewhat eliviates the problem:
https://github.com/D-Programming-Language/phobos/pull/1364

But it restricts the range to bidirectional (for the reasons above).

Actually, it's mimited to Bidir, but if you *do* use it as bidirectional, then 1 element will be evaluated twice. So I'm wondering if it might not be better to simply restrict it to forward...

Reply via email to