On Monday, 4 May 2015 at 14:33:23 UTC, Baz wrote:
the following program fails because of the `put` function :

---
import std.stdio;
import std.range;

size_t readN(T, Range)(ref Range src, ref Range dst, size_t n)
if (isInputRange!Range && isOutputRange!(Range, T))
{
    size_t result;

    while(1)
    {
        if (src.empty || result == n)
            break;

        put(dst, src.front()); // here
        src.popFront;

        ++result;
    }


    return result;
}

void main(string[] args)
{
    int[] src = [1,2,3];
    int[] dst = [0];

    auto n = readN!int(src, dst, 2);

    writeln(dst);
}
---

If i replace `put` by a cat op (`~`) it works, however the cat op only works here because i test the template with two int[].

What's wrong ?

I believe the put(R,E) calls the doPut(R, E)


private void doPut(R, E)(ref R r, auto ref E e)
{
//...
 else static if (isInputRange!R)
{
   static assert(is(typeof(r.front = e)),
"Cannot nativaly put a " ~ E.stringof ~ " into a " ~ R.stringof ~ ".");
   r.front = e;
   r.popFront();
}
//...
}


So basically you put elements into the list until it is empty.


import std.stdio;
import std.range;

void main()
{
   int [] list = new int [10];
   writeln(list);    //10 zeros
   list.front = 5;   //1 five and 9 zeroes
   writeln(list);
   list.popFront();  //9 zeroes left.
   writeln(list);
}







Reply via email to