error with std.range.put - readN

Ali Çehreli via Digitalmars-d digitalmars-d at puremagic.com
Mon May 4 11:50:45 PDT 2015


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.

Ali



More information about the Digitalmars-d mailing list