On 08/26/2012 08:41 AM, David wrote:
It's a RefRange, but not completly ... Can somebody explain me that
behaviour?

http://dpaste.dzfl.pl/643de2a3

According to its documentation, RefRange works differently whether the original range is a ForwardRange or not:

  http://dlang.org/phobos/std_range.html#refRange

I have made TestRange a ForwardRange but then I had to comment out two lines of your program. Does it work according to your expectations with this change?

import std.stdio;
import std.range;

struct TestRange {
    float[] x = [0, 1, 2, 3, 4, 5];

    @property bool empty() {
        return x.length == 0;
    }

    @property ref float front() {
        return x[0];
    }

    void popFront() {
        //writefln("before: %s", x);
        x = x[1..$];
        //x.popFront();
        //writefln("after: %s", x);
    }

    TestRange save() @property {
        return TestRange(x);
    }
}



void main() {
    static assert(isForwardRange!TestRange);

    TestRange r = TestRange();
    auto rr = refRange(&r);

    //    foreach(element; rr) {}

    //    writefln("Original range: %s", r.x);
    //    writefln("RefRange: %s", rr.x);

    writefln("%s - %s", r.x.ptr, r.x.ptr);

    rr.popFront();

    writefln("%s - %s", r.x.ptr, r.x.ptr);
    writefln("Original range: %s", r.x);
// We can't expect the RefRange to have the members of the original range
    // writefln("RefRange: %s", rr.x);

    r.popFront();

    writefln("%s - %s", r.x.ptr, r.x.ptr);
    writefln("Original range: %s", r.x);
// We can't expect the RefRange to have the members of the original range
    // writefln("RefRange: %s", rr.x);
}

Ali

Reply via email to