Challenge: write a reference counted slice that works as much as possible like a built-in slice

Atila Neves atila.neves at gmail.com
Tue Nov 9 15:37:13 UTC 2021


On Tuesday, 9 November 2021 at 15:26:24 UTC, Dukc wrote:
> On Tuesday, 9 November 2021 at 11:56:50 UTC, Atila Neves wrote:
>> I'll hijack this thread with a slide from my DConf Online 2020 
>> talk, because with the current language it's impossible for a 
>> library writer to write a useful @safe vector struct:
>>
>> ```d
>> scope v = vector(1, 2, 3, 4);
>> scope s = v[];
>> v ~= 5;  // could reallocate here
>> s[3] = 42;  // oops
>> ```
>
> Hm, if the vector had a reference count

Then it wouldn't be a vector.

> Functions like these could also solve the thing if only called 
> from `@live`:
>
> ```d
> AppendableVector setupAppending(Vector*);
> Vector stopAppending(AppendableVector*);
> ```
>
> Obviously, you could not slice an AppendableVector, and we're 
> not proposing that average application code should be `@live` 
> so not a very good solution.

The main point of vector is that one can append to it without 
using the GC. I guess that "this thing has a length only known at 
runtime that won't change and doesn't use the GC" is useful too, 
but not nearly as much as being able to append.


More information about the Digitalmars-d mailing list