Question about OutputRange and std.range: put

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 16 09:49:31 PST 2016


On Saturday, January 16, 2016 16:53:24 Uranuz via Digitalmars-d-learn wrote:
> On Saturday, 16 January 2016 at 16:14:56 UTC, Jonathan M Davis
> wrote:
> > On Saturday, January 16, 2016 12:11:11 Uranuz via
> > Digitalmars-d-learn wrote:
> >> [...]
> >
> > There are a few problems here. First off, when put is used with
> > an array, it fills the array. It doesn't append to it. So, you
> > can't use a string as an output range, since its elements are
> > immutable. Here's an example with int[]:
> >
> > [...]
>
> Thanks for your response. After looking for some time in Phobos
> documentation I already found that it would be better to use
> Appender for this purpose. As far as I understand it allocates
> less often and I can reserve some memory from the start. I'll
> take this possibility into account when I'll do further small
> optimisations. Thanks again.

Where using an array rather than Appender makes sense is when you want to
allocate a buffer and the reuse it over and over again rather than
allocating a new array and then growing it efficiently each time. But then
you have to be careful about how you use it, since arrays are quirkier when
using them as output ranges - and unlike Appender, they can fill up. So,
it's a question of what you're trying to do and what's reasonable in your
program.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list