So how exactly does one make a persistent range object?

Ali Çehreli acehreli at yahoo.com
Mon Jun 13 16:03:55 PDT 2011


On Sat, 04 Jun 2011 20:27:16 +0200, Andrej Mitrovic wrote:

> This is my #1 problem with ranges right now:
> 
> import std.range;
> 
> int[3] a = [1, 2, 3];
> shared range = cycle(a[]);  // nope
> 
> void main()
> {
>     foo();
> }
> 
> void foo()
> {
>     // do something with range
> }
> 
> test.d(6): Error: static variable a cannot be read at compile time
> test.d(6): Error: cannot evaluate cycle(a[]) at compile time

Has this been answered? The problem is with 'a'. Defining it as enum 
fixes that problem:

import std.stdio;
import std.range;

enum a = [1, 2, 3];
auto range = cycle(a[]);

void main()
{
    foreach (i; 0 .. 2) {
        foo();
    }
}

void foo()
{
    foreach(i; 0 .. 5) {
        writeln(range.front);
        range.popFront();
    }
}

Ali


More information about the Digitalmars-d-learn mailing list