Remove elements without losing capacity

Riccardo M asd at asd.asd
Tue Oct 4 17:59:31 UTC 2022


On Tuesday, 4 October 2022 at 15:42:21 UTC, Steven Schveighoffer 
wrote:
> Yes, you use `assumeSafeAppend`:
>
> ```d
> arr.length--;
> arr.assumeSafeAppend;
> assert(arr.capacity != 0);
> ```
>
> Now, I want to clarify that you should only use this if you are 
> sure you are done with the data you removed at the end, as it 
> will get overwritten upon more appends to the array.
>
> [...]

Thanks! I can use the unstable `remove` and then restore the 
original capacity with `assumeSafeAppend` in case data can be 
safely overwritten.

The inherent reason for `remove` to cancel previous capacity and 
requiring new allocations is exactly to prevent overwriting data 
that could be owned by something else?


More information about the Digitalmars-d-learn mailing list