Design with appender: good or bad?

Chris wendlec at tcd.ie
Thu Apr 10 03:16:42 PDT 2014


On Thursday, 10 April 2014 at 10:00:16 UTC, John Colvin wrote:
> On Thursday, 10 April 2014 at 09:47:35 UTC, Chris wrote:
>> Are there any drawbacks with this design, i.e. using buf.put() 
>> here
>> (instead of a less efficient items ~= item;)?
>>
>> struct MyStruct {
>>  Appender!(string[]) buf;
>>  string name;
>>
>>  this(string name) {
>>    this.name = name;
>>    buf = appender!(string[]);
>>  }
>>
>>  public addItem(string item) {
>>    buf.put(item);
>>  }
>>
>>  @property string[] items() {
>>    return buf.data;
>>  }
>> }
>
> Appender supports ~ and ~=, so you can have your cake and eat 
> it.

Ah, this one \me (= escaped me). Sorry I wasn't precise, I was 
wondering if it is in any way dangerous to have

@property string[] items() {
   return buf.data;
}

instead of:

string[] items;
// ...
public addItem(string item) {
   items ~= item;
}

because the two are not the same. When you print MyStruct to 
console you get the address of appender buf, whereas when you use 
string[] items you get the actual items. I'm only wondereing if 
using appender is potentially dangerous and could bite me later.


More information about the Digitalmars-d-learn mailing list