Appenders and Arrays

default0 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 1 09:49:27 PDT 2015


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"?

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)
2) Why put removes elements from arrays

Hopefully somebody can help me clear my confusion about this (yes 
in the meantime I can just use Appenders, but you know, still 
wondering).


More information about the Digitalmars-d-learn mailing list