Random access range

Xinok xinok at live.com
Thu Mar 8 09:04:37 PST 2012


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); }
}


More information about the Digitalmars-d mailing list