Is there something special required to use Appender.clear

Anonymouse asdf at asdf.net
Tue Mar 27 12:27:14 UTC 2018


On Tuesday, 27 March 2018 at 12:17:58 UTC, Ellie Harper wrote:
> Sorry if this is a stupid question, but is there something 
> special required to call Appender.clear?  When I attempt even 
> just a simple use I am getting compile errors relating to 
> `template object.clear`.
>
> When I try:
>
>     import std.array;
>
>     void main(string[] args){
>       auto foo = appender!string;
>       foo.clear;
>     }

Appender only implements clear for mutable types, and string is 
an array of immutables. I'm not sure but it looks like you're 
hitting the associative array clear by UFCS instead?

Make it Appender!(char[]) and it will work.

> // only allow overwriting data on non-immutable and non-const 
> data
> static if (isMutable!T)
> {
>     /**
>       * Clears the managed array.  This allows the elements of 
> the array to be reused
>       * for appending.
>       *
>       * Note: clear is disabled for immutable or const element 
> types, due to the
>       * possibility that $(D Appender) might overwrite 
> immutable data.
>       */
>      void clear() @trusted pure nothrow
>      {
>          if (_data)
>          {
>              _data.arr = _data.arr.ptr[0 .. 0];
>          }
>      }
> [...]

https://github.com/dlang/phobos/blob/master/std/array.d#L3140


More information about the Digitalmars-d-learn mailing list