On 9/1/15 12:49 PM, default0 wrote:
Hello

A simple thing I stumbled across:

int main()
{
     import std.stdio;
     import std.range;

     int[] d;
     d ~= 10;
     d ~= 20;
     d.put(5);
     writeln(d);

     return 0;
}

Appenders work fine as output ranges, but arrays do not. The above code
prints "20" (ie the 10 is removed). Is "put" not supposed to mean
"append one element"?

put into an slice does not append, it fills in the front. This is because the "target" of a slice is the data it points at.

Think of it as a buffer that you want to fill:

int[20] buf;
int[] outputRange = buf[];
outputRange.put(10);
outputRange.put(20);

assert(buf[0..2] == [10,20]);

Further, while the following compiles fine but runs really odd, the
following does not compile:

int main()
{
     import std.stdio;
     import std.range;

     char[] c;
     c ~= 'a';
     c ~= 'b';
     c.put('c');
     writeln(c);

     return 0;
}

C:\dmd\src\phobos\std\range.d(9,9): Error: static assert  "Cannot put a
char into a char[]." (Test)

I am puzzled by
1) Why I cannot put a char into a char[] (even though I can totally
append them)

That seems like a bug. put has specific code to deal with putting characters into character ranges. Please file https://issues.dlang.org/enter_bug.cgi

-Steve

Reply via email to