www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.array : appender woes

reply abad <abad.flt gmail.com> writes:
import std.array : appender;
import std.stdio : writeln;

void main() {
     auto app = appender!(char[]);
     app.put('x');
     auto foo = app.data;
     app.clear; // done, start a new array
     app.put('y');
     writeln(foo);
}

This prints out 'y'. It's not surprising because what I suppose 
app.data is doing is just returning a slice of the dynamic array. 
Clear() resets the counter of current position. But this behavior 
is confusing in practical use, IMHO.

Should calling clear in your opinion guarantee that a new array 
gets allocated?
Jun 16 2016
parent reply cym13 <cpicard openmailbox.org> writes:
On Thursday, 16 June 2016 at 07:47:03 UTC, abad wrote:
 import std.array : appender;
 import std.stdio : writeln;

 void main() {
     auto app = appender!(char[]);
     app.put('x');
     auto foo = app.data;
     app.clear; // done, start a new array
     app.put('y');
     writeln(foo);
 }

 This prints out 'y'. It's not surprising because what I suppose 
 app.data is doing is just returning a slice of the dynamic 
 array. Clear() resets the counter of current position. But this 
 behavior is confusing in practical use, IMHO.

 Should calling clear in your opinion guarantee that a new array 
 gets allocated?
I don't find it confusing at all, what did you expect?
Jun 16 2016
parent reply abad <abad.flt gmail.com> writes:
On Thursday, 16 June 2016 at 07:59:50 UTC, cym13 wrote:
 On Thursday, 16 June 2016 at 07:47:03 UTC, abad wrote:
 import std.array : appender;
 import std.stdio : writeln;

 void main() {
     auto app = appender!(char[]);
     app.put('x');
     auto foo = app.data;
     app.clear; // done, start a new array
     app.put('y');
     writeln(foo);
 }

 This prints out 'y'. It's not surprising because what I 
 suppose app.data is doing is just returning a slice of the 
 dynamic array. Clear() resets the counter of current position. 
 But this behavior is confusing in practical use, IMHO.

 Should calling clear in your opinion guarantee that a new 
 array gets allocated?
I don't find it confusing at all, what did you expect?
Consider the word clear in this context. What is it that gets cleared? The data in the array (which might imply reallocation)? Nope, what gets "cleared" is the index to current position in the array. I think 'reset' may have been a better name for the function.
Jun 16 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/16/16 6:08 AM, abad wrote:
 On Thursday, 16 June 2016 at 07:59:50 UTC, cym13 wrote:
 On Thursday, 16 June 2016 at 07:47:03 UTC, abad wrote:
 import std.array : appender;
 import std.stdio : writeln;

 void main() {
     auto app = appender!(char[]);
     app.put('x');
     auto foo = app.data;
     app.clear; // done, start a new array
     app.put('y');
     writeln(foo);
 }

 This prints out 'y'. It's not surprising because what I suppose
 app.data is doing is just returning a slice of the dynamic array.
 Clear() resets the counter of current position. But this behavior is
 confusing in practical use, IMHO.

 Should calling clear in your opinion guarantee that a new array gets
 allocated?
I don't find it confusing at all, what did you expect?
Consider the word clear in this context. What is it that gets cleared? The data in the array (which might imply reallocation)? Nope, what gets "cleared" is the index to current position in the array. I think 'reset' may have been a better name for the function.
In fact, what gets cleared is the data in the array. You have a dangling reference to the array, for which the data is not technically valid. -Steve
Jun 16 2016