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

Dukc ajieskola at gmail.com
Tue Nov 9 15:26:24 UTC 2021


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 and would crash if 
appended to when the count is non-zero?

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.

>
> I initialy "solved" this with @trusted, but fortunately 
> somebody on the forums pointed out I was being an idiot.

If that makes anybody idiot, I doubt there are many normal-minded 
programmers. Let alone geniuses.




More information about the Digitalmars-d mailing list