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

Steven Schveighoffer schveiguy at gmail.com
Mon Mar 25 17:32:13 UTC 2019


On 3/25/19 1:06 PM, Meta wrote:
> 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?

I have a feeling it's an aliasing thing -- like every app member in 
every class points at the same IMPL struct.

-Steve


More information about the Digitalmars-d mailing list