www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Random access range

reply "zeljkog" <zeljkog home.com> writes:
import std.stdio;

struct Rar{
   int[] data = [1,3,5];
   int length = 3;
   ref int opIndex(int i){ return data[i];}
}

void main() {
   Rar x;
   foreach (e; x)
   writeln(e);
}

Error: invalid foreach aggregate x
----

Is'nt Rar valid random access range?
Mar 08 2012
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 08 Mar 2012 11:43:24 -0500, zeljkog <zeljkog home.com> wrote:

 import std.stdio;

 struct Rar{
    int[] data = [1,3,5];
    int length = 3;
    ref int opIndex(int i){ return data[i];}
 }

 void main() {
    Rar x;
    foreach (e; x)
    writeln(e);
 }

 Error: invalid foreach aggregate x
 ----

 Is'nt Rar valid random access range?

No, a random access range must also be a bidirectional and input range. You need the standard range primitives. -Steve
Mar 08 2012
prev sibling parent "Xinok" <xinok live.com> writes:
On Thursday, 8 March 2012 at 16:43:25 UTC, zeljkog wrote:
 import std.stdio;

 struct Rar{
   int[] data = [1,3,5];
   int length = 3;
   ref int opIndex(int i){ return data[i];}
 }

 void main() {
   Rar x;
   foreach (e; x)
   writeln(e);
 }

 Error: invalid foreach aggregate x
 ----

 Is'nt Rar valid random access range?

struct N(T){ T[] arr; this(T[] other){ arr = other; } this(N other){ arr = other.arr; } // The declarations below are required // The property tag is also required ref T opIndex(size_t i){ return arr[i]; } property size_t length(){ return arr.length; } property ref T front(){ return arr.front; } property ref T back(){ return arr.back; } void popFront(){ arr.popFront(); } void popBack(){ arr.popBack(); } property bool empty(){ return arr.empty; } property N save(){ return N(arr); } }
Mar 08 2012