No property 'clear/shrinkTo' for type 'Appender!(string)'
Steven Schveighoffer
schveiguy at yahoo.com
Thu Nov 11 13:37:27 PST 2010
On Thu, 11 Nov 2010 15:33:41 -0500, Eric Desbiens <olace.mail at gmail.com>
wrote:
> Hello,
>
> If I create an appender for a string, I cannot call the funtion clear or
> shrinkTo. For example:
>
> auto strAppender = appender!string();
> strAppender.clear();
> strAppender.shrinkTo();
>
> gives compilation error. In std.array source code, it seems that it
> allows
> those function to work only on types that are not immutable. But on a
> normal
> string, I can change the length with no problem or reinitialize it.
>
> I am pretty new to D and I just want to be sure that this is a bug before
> reporting it in bugzilla or if this is by design.
It's not a bug, it's to prevent immutable data from being mutated.
When you shrink the length of a builtin string, it does work, but any
appending from that point will create a copy of that string to append to.
Appender is built for appending and so is more focused on appending
in-place. Therefore, clear and shrinkTo allow you to overwrite the data
that had already been valid. This is no good with immutable data.
As a solution, you can simply create a new appender:
strAppender = strAppender.init;
However, that seems limited, now that I look at it. It may be better to
allow clear at least to simply reset the appended-to array to null, and
allow you to start appending again.
Go ahead and file a bug, and mark it as an enhancement.
-Steve
More information about the Digitalmars-d
mailing list