How to append range to array?
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat May 23 01:27:55 PDT 2015
On Saturday, May 23, 2015 07:03:33 Vladimir Panteleev via Digitalmars-d-learn wrote:
> int[] arr = [1, 2, 3];
> auto r = iota(4, 10);
> // ???
> assert(equal(arr, iota(1, 10)));
>
> Hopefully in one GC allocation (assuming we know the range's
> length).
>
> I tried std.range.primitives.put but its behavior seems a little
> mysterious:
>
> This compiles but asserts at runtime:
>
> int[] arr = [1, 2, 3];
> arr.put(iota(4, 10));
>
> And this is even weirder, can you guess what it will print?
>
> int[] arr = [1, 2, 3];
> arr.put(4);
> writeln(arr);
For better or worse, put does not append to arrays. It fills them. If you
want to append using put, then using std.array.Appender.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list