A Riddle: what is wrong with this code using std.array.Appender?

Meta jared771 at gmail.com
Mon Mar 25 17:06:12 UTC 2019


On Monday, 25 March 2019 at 14:58:08 UTC, FeepingCreature wrote:
> class Class
> {
>     Appender!(int[]) app = null;
> }
>
> This is the most evil bug I've seen this year yet.
>
> Hint: Appender is a struct, not a class. So what does "= null" 
> do, when it appears as a default initializer?

I can't see the bug in this minimal example:

import std.array: Appender;

class Class
{
     Appender!(int[]) app = null;
}

void main()
{
     auto c = new Class();
     import std.stdio;
     writeln(c.app.data); //Prints "[]"
     c.app ~= 10;
     writeln(c.app.data); //Prints "[10]"
}

What's the problem?


More information about the Digitalmars-d mailing list