The OpenD Programming Language

isForwardRange

Returns true if R is a forward range. A forward range is an input range r that can save "checkpoints" by saving r.save to another value of type R. Notable examples of input ranges that are not forward ranges are file/socket ranges; copying such a range will not save the position in the stream, and they most likely reuse an internal buffer as the entire stream does not sit in memory. Subsequently, advancing either the original or the copy will advance the stream, so the copies are not independent.

The following code should compile for any forward range.

static assert(isInputRange!R);
R r1;
auto s1 = r1.save;
static assert(is(typeof(s1) == R));

Saving a range is not duplicating it; in the example above, r1 and r2 still refer to the same underlying data. They just navigate that data independently.

The semantics of a forward range (not checkable during compilation) are the same as for an input range, with the additional requirement that backtracking must be possible by saving a copy of the range object with save and using it later.

save behaves in many ways like a copy constructor, and its implementation typically is done using copy construction.

The existence of a copy constructor, however, does not imply the range is a forward range. For example, a range that reads from a TTY consumes its input and cannot save its place and read it again, and so cannot be a forward range and cannot have a save function.

  1. enum bool isForwardRange(R);
    enum bool isForwardRange(R);
  2. enum bool isForwardRange(R, E);
  3. import std.traits;
  4. enum bool isInputRange(R, E);

Examples

static assert(!isForwardRange!(int));
static assert( isForwardRange!(int[]));
static assert( isForwardRange!(inout(int)[]));

static assert( isForwardRange!(int[], const int));
static assert(!isForwardRange!(int[], immutable int));

static assert(!isForwardRange!(const(int)[], int));
static assert( isForwardRange!(const(int)[], const int));
static assert(!isForwardRange!(const(int)[], immutable int));

static assert(!isForwardRange!(immutable(int)[], int));
static assert( isForwardRange!(immutable(int)[], const int));
static assert( isForwardRange!(immutable(int)[], immutable int));
alias R = int[];
R r = [0,1];
static assert(isForwardRange!R);           // is forward range
r.popBack();                               // can invoke popBack
auto t = r.back;                           // can get the back of the range
auto w = r.front;
static assert(is(typeof(t) == typeof(w))); // same type for front and back

See Also

The header of std.range for tutorials on ranges.

Meta