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

Paul Backus snarwin at gmail.com
Wed Nov 10 14:47:34 UTC 2021


On Wednesday, 10 November 2021 at 13:48:53 UTC, deadalnix wrote:
> On Tuesday, 9 November 2021 at 17:04:29 UTC, Paul Backus wrote:
>> Here's a basic sketch of the scheme I had in mind: 
>> https://gist.github.com/run-dlang/d1982a29423b2cb545bc9fa452d94c5e
>>
>> It's entirely possible I've overlooked something and this is 
>> actually unsound. As Andrei says: "destroy!"
>
> There are a few problems with the code, indeed.
> 1/ You need to add runtime check, because in D, destructed 
> objects may not have been constructed, so you need to be able 
> to destroy .init . in your case, pointers will be null and 
> you'll segfault.

Destroying .init works fine here. `free(null)` is guaranteed by 
the C standard to be a no-op.

> 2/ For a struct, you cannot guarantee that the struct's method 
> don't escape the struct pointer, so none of this can be safe in 
> any way.

Good point. I guess you'd need transitive `scope` for this, at 
minimum.


More information about the Digitalmars-d mailing list