www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - So how exactly does one make a persistent range object?

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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

If I want to create a range once during application startup or during
some function call and then use it throughout the lifetime of the app
I need to store the range object either in module scope or inside some
class/struct that I can pass around. But I have no way of declaring
the type:

import std.range;

int[3] a = [1, 2, 3];
shared Cycle range;  // nope

void main()
{
    range = cycle(a[]);
    foo();
}

void foo()
{
    // do something with range
}

Error: struct std.range.Cycle(Range) if
(isForwardRange!(Unqual!(Range)) && !isInfinite!(Unqual!(Range))) is
used as a type

I can't even construct a range as a static variable inside a function:
import std.range;

int[3] a = [1, 2, 3];

void main()
{
    foo();
}

void foo()
{
    static range = cycle(a[]);  // nope
    // do something with range
}

test.d(14): Error: static variable a cannot be read at compile time
test.d(14): Error: cannot evaluate cycle(a[]) at compile time
Jun 04 2011
parent Ali =?iso-8859-1?q?=C7ehreli?= <acehreli yahoo.com> writes:
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
Jun 13 2011