www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Strange appender behavior

reply tchaloupka <chalucha gmail.com> writes:
Is this expected?:

```
import std.stdio;
import std.algorithm;
import std.array;

void main()
{
     auto d = Appender!string();
     //auto d = appender!string(); // works

     string[] arr = ["foo", "bar", "baz"];
     arr.joiner("\n").copy(d);
     writeln(d.data);
}
```

Using Appender outpust nothing, using appender works ok.
Mar 13 2019
next sibling parent Andrea Fontana <nospam example.org> writes:
On Wednesday, 13 March 2019 at 13:03:27 UTC, tchaloupka wrote:
 Is this expected?:

 ```
 import std.stdio;
 import std.algorithm;
 import std.array;

 void main()
 {
     auto d = Appender!string();
     //auto d = appender!string(); // works

     string[] arr = ["foo", "bar", "baz"];
     arr.joiner("\n").copy(d);
     writeln(d.data);
 }
 ```

 Using Appender outpust nothing, using appender works ok.
It sounds like a bug. If you use Appender!string(null) it works fine. Probably a problem with _data init?
Mar 13 2019
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 13 March 2019 at 13:03:27 UTC, tchaloupka wrote:
 Is this expected?:
You never called the constructor, which means it was lazy initialized... but that was done inside one of the functions, which received the Appender by value. So when it set its internal pointer, it was inside a function and never got seen outside. Similar to if you do char* a; void foo(char* a) { a = new char[](10); } foo(a); If you were to put something before the other calls, it would work. Or call the constructor (which the little-a appender function does). I'm kinda of the opinion this should be a compile error; that it should force you to call the constructor, so that could be a minor bug, but it isn't unexpected per se since regular pointers and arrays work the same way.
Mar 13 2019