Can assumeSafeAppend() grab more and more capacity?
Jesse Phillips via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jun 6 12:13:41 PDT 2017
On Monday, 5 June 2017 at 23:17:46 UTC, Ali Çehreli wrote:
> auto a = [ 1, 2, 3, 4 ];
> auto b = a;
>
> Both of those slices have non-zero capacity yet one of them
> will be the lucky one to grab it. Such semantic issues make me
> unhappy. :-/
>
> Ali
You have to remember that slices don't own their memory. So while
capacity show a guaranteed reserved memory, it is reserved for
the dynamic array the slice has a window into.
Remove probably shouldn't try to reclaim capacity, while it is
destructive for any other slice, it shouldn't make string
appending also destructive.
untested:
auto a = [ 1, 2, 3, 4 ];
auto b = a[$-1, $];
a.remove(2);
assert(b == [4]);
a ~= 6;
assert(b == [4]);
More information about the Digitalmars-d-learn
mailing list