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 11:56:50 UTC 2021
On Monday, 8 November 2021 at 21:42:12 UTC, Andrei Alexandrescu
wrote:
> This was prompted by this exchange with Paul Backus that went
> like this:
>
> Me: "We never got reference counting to work in safe pure nogc
> code. And we don't know how. If we did, we could write a
> slice-like type that does everything a slice does PLUS manages
> its own memory. This is the real problem with containers."
>
> Paul: "I'm pretty sure at least some of us (including Atila)
> know how."
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
```
I initialy "solved" this with @trusted, but fortunately somebody
on the forums pointed out I was being an idiot.
It might just be my biases but I think this is a more useful nut
to crack. And the only way to crack it is to some sort of
ownership system, since there's no way currently in D to have a
compile-time guarantee that there's only one alias left.
"We can have @safe vectors, unless you actually want to append"
isn't a great slogan. And vectors are clearly simpler than ref
counted arrays.
More information about the Digitalmars-d
mailing list