Passing Appender by value

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat Jun 22 06:48:40 PDT 2013


On 6/22/13, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
>     Appender!(int[]) buffer;
>     call(buffer);
>     writeln(buffer.data);  // writes [], it's empty

Apparently it's the same thing as the old AA problem. Essentially the
buffer would have to be initialized first by appending:

-----
import std.array;
import std.stdio;

void call(Appender!(int[]) buffer)
{
    buffer.put(1);
}

void main()
{
    Appender!(int[]) buffer;
    call(buffer);
    assert(buffer.data.empty);  // passes

    call(buffer);
    assert(buffer.data.empty);  // still passes

    buffer.put(2);
    call(buffer);
    assert(buffer.data == [2, 1]);  // now it finally went through
}
-----

It has something to do with null-initialization. I remember the
discussion about this problem with hashes, I just can't remember if
there was a bug report about it to link to.


More information about the Digitalmars-d-learn mailing list