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

Paul Backus snarwin at gmail.com
Tue Nov 9 03:43:01 UTC 2021


On Tuesday, 9 November 2021 at 01:14:24 UTC, tsbockman wrote:
> Borrowing is required for a general-purpose RC type, so that 
> the payload can actually be used without a reference to the 
> payload escaping outside the lifetime of the counting 
> reference. But, the effective lifetime of the counting 
> reference is not visible to the `scope` borrow checker, because 
> at any point the reference's destructor may be manually called, 
> potentially `free`ing the payload while there is still an 
> extant borrowed reference.
>
> With current language semantics, the destructor (and any other 
> similar operations, such as reassignment) of the reference type 
> must be `@system` to prevent misuse of the destructor in 
> `@safe` code.
>     https://issues.dlang.org/show_bug.cgi?id=21981
>
> The solution to this problem is to introduce some way of 
> telling the compiler, "this destructor is `@safe` to call 
> automatically at the end of the object's scope, but `@system` 
> to call early or manually."

I believe it is also possible to make this `@safe` by doing 
borrow checking at runtime, although it would introduce some 
overhead, and make the API less ergonomic.

Maybe the best compromise we can reach in the current language is 
to offer a `@safe` borrow-checked interface alongside an 
unchecked `@system` interface.


More information about the Digitalmars-d mailing list