Alias this with array can only be used once
Simen Kjærås
simen.kjaras at gmail.com
Wed Feb 22 14:28:47 PST 2012
On Wed, 22 Feb 2012 23:16:41 +0100, Blake Anderton <rbanderton at gmail.com>
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
> }
That's interesting. The thing that happens is the function popFront
is callable on ArrayContainer, and popFront mutates its parameter.
The solution is to use count(c[]), but I'm not so sure I agree with
the way this works currently.
More information about the Digitalmars-d-learn
mailing list