DList.Range magically becomes empty.

sclytrack via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 25 01:35:08 PST 2015


On Wednesday, 25 February 2015 at 09:07:17 UTC, Tobias Pankrath 
wrote:
> import std.container;
> import std.stdio;
>
>
> void main()
> {
>    DList!int list;
>    Array!(DList!int.Range) stack;
>    foreach(i; 0 .. 4)
>    {
>       list.stableInsertBack(i);
>       stack.insertBack(list[]);
>    }
>
>    writefln("list: %s", list[]); // fine
>    writefln("stack: %s", stack[]); //fine
>
>    foreach(s; stack[])
>       writefln("s: %s", s); // all s are empty?
>
>    writefln("stack: %s", stack[]); //not fine
> }
>
> It prints:
>
> list: [0, 1, 2, 3]
> stack: [[0], [0, 1], [0, 1, 2], [0, 1, 2, 3]]
> s: []
> s: []
> s: []
> s: []
> stack: [[], [], [], []]

Ranges aren't containers. It's more like a view of a container 
that gets smaller (or empty) the more you use it. There might not 
be a container behind a range.




More information about the Digitalmars-d-learn mailing list