www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6193] New: Appender.clear() functionality or documentation

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6193

           Summary: Appender.clear() functionality or documentation
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



This is an enhancement request, or (because I am often mistaken) it's just an
enhancement request for the documentation.

This program works with DMD 2.053:

import std.stdio;
void main() {
    string foo = "abc";
    foo ~= 'a';
    foo.length = 0;
}


This other program too runs:

import std.array: Appender;
void main() {
    Appender!(char[]) foo;
    foo.put('a');
    foo.clear();
}


But this gives an error:
temp.d(5): Error: no property 'clear' for type 'Appender!(string)'


import std.array: Appender;
void main() {
    Appender!string foo;
    foo.put('a');
    foo.clear();
}



Strings are made of immutable chars, but you are allowed to clear (set the
length to zero) a string. So I expect Appender to allow the same.

If this not a good thing, then I suggest to add to the online docs of the
std.array.Appender.clear() method a note that explains it is present
(compiled-in) only in certain cases. Because currently I haven't seen a mention
of this, and the error message given by the compiler doesn't explain enough.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 21 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6193


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
         AssignedTo|nobody puremagic.com        |schveiguy yahoo.com
           Severity|enhancement                 |minor



06:02:18 PDT ---
It is intentionally missing from Appender when the data type is immutable or
const.  See the code here:

https://github.com/D-Programming-Language/phobos/blob/master/std/array.d#L1870

The issue is because Appender owns the data it's appending to, clearing the
data will *reuse* the buffer to do appending again.  But this would violate the
requirements for immutable.  This is different from setting a slice length to
0, because a slice *does not own* the data.  Appending to a slice whose length
was set to 0 would allocate a *new* block to append into.

The equivalent slice code for Appender.clear is:

slice.length = 0;
slice.assumeSafeAppend();

There is a reason const is also included.  Appender can take an *existing*
buffer and use it as the base for appending.  Since Appender!(const(T)[]) can
accept an immutable(T)[], you would have the same issue if you then called
clear on the data.

I'll note that clear is not enabled when the Appender is for const or immutable
arrays in the documentation.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 22 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6193


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED



06:15:52 PDT ---
I expect this to be pulled momentarily, it's just documentation:

https://github.com/D-Programming-Language/phobos/pull/116

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 22 2011