Output ranges and arrays
Steven Schveighoffer
schveiguy at yahoo.com
Fri Nov 12 06:30:29 PST 2010
On Fri, 12 Nov 2010 09:22:20 -0500, Olivier Pisano
<olivier.pisano at laposte.net> wrote:
> Hi,
>
> I am starting to play with output ranges and have trouble understanding
> how they do work on arrays. Consider the following code :
>
> import std.array;
> import std.range;
> import std.stdio;
>
> void main(string[] argv)
> {
> auto a = [1, 2, 3];
> a.put(4);
>
> writefln("%s", a);
> }
>
> One could expect the call to put() to append 4 to the array so the array
> content would be [1, 2, 3, 4].
> Instead of this, I get "[2, 3]" to be printed. So I guess put() is
> translated to
>
> r.front = e; r.popFront();
>
> as written in std.range.put documentation.
>
> Is it the expected behaviour or is it a bug ?
Expected. If you want an appendable array as an output range, use
std.array.Appender.
auto a = appender([1,2,3]);
a.put(4);
writefln("%s", a.data);
-Steve
More information about the Digitalmars-d
mailing list