So how exactly does one make a persistent range object?

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat Jun 4 11:27:16 PDT 2011


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


More information about the Digitalmars-d-learn mailing list