| Thread overview | |||||
|---|---|---|---|---|---|
|
March 08, 2012 Random access range | ||||
|---|---|---|---|---|
| ||||
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?
| ||||
March 08, 2012 Re: Random access range | ||||
|---|---|---|---|---|
| ||||
Posted in reply to zeljkog | 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
| |||
March 08, 2012 Re: Random access range | ||||
|---|---|---|---|---|
| ||||
Posted in reply to zeljkog | 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); }
}
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply