June 04, 2011 So how exactly does one make a persistent range object? | ||||
---|---|---|---|---|
| ||||
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 |
June 13, 2011 Re: So how exactly does one make a persistent range object? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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
|
Copyright © 1999-2021 by the D Language Foundation