On 02/22/2012 02:16 PM, Blake Anderton wrote:
Why doesn't this work? I'm assuming I'm not fully understanding how
"alias this" interacts with ranges (i.e. the range isn't being reset
after count is finished with it), but I'm not sure how to fix this either:

import std.algorithm;

class ArrayContainer
{
int[] values;
this(int[] v) { values = v; }
alias values this;
}

void main(string[] args)
{
auto c = new ArrayContainer([1, 2, 3]);
assert(count(c) == 3); //succeeds
assert(c.length == 3); //FAILS - is actually zero
}

Consumption of the range is natural for an InputRange.

What I see in Phobos is that containers like ArrayContainer themselves don't behave like ranges, rather they hand out Range objects to be consumed.

As a solution, it can have a member function named something like elements() that returns a separate array to be used as an InputRange.

Ali

Reply via email to