error with std.range.put - readN

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Mon May 4 12:16:46 PDT 2015


On Monday, 4 May 2015 at 18:50:45 UTC, Ali Çehreli wrote:
> On 05/04/2015 07:33 AM, Baz wrote:
>
> >      int[] src = [1,2,3];
> >      int[] dst = [0];
>
> In addition to what others said, even if dst had room for two 
> elements, it would lose the newly added element due to a 
> popFront() called implicitly during put()'ting.
>
>     int[] dst = [0, 0];    // <-- Has room now
>
>     auto n = readN!int(src, dst, 2);
>
>     writeln(dst);          // <-- Prints "[]" WAT?
>
> One solution is to introduce another slice that would not be 
> popFront'ed:
>
>     int[] dst = [0, 0];
>     int[] dst2 = dst;    // <-- This
>
>     auto n = readN!int(src, dst, 2);
>
>     writeln(dst2);       // <-- prints "[1, 2]"
>
> However, Appender is a better solution.

I really think that output ranges need to be rethought through. 
In most cases, lazy input ranges should be used instead, but they 
do have their uses. The problem is that they're not designed well 
enough or thoroughly enough to be very useful outside of 
Appender. Using arrays as output ranges is particularly bad, but 
the fact that there is no way to know whether an output range 
even has room to put a new element or range of elements onto its 
end makes output ranges unusable in many cases IMHO.

- Jonathan M Davis


More information about the Digitalmars-d mailing list