Allocator-aware @safe reference counting is still not possible

FeepingCreature feepingcreature at gmail.com
Thu Jan 26 06:58:01 UTC 2023


On Thursday, 26 January 2023 at 06:55:41 UTC, FeepingCreature 
wrote:
> On Monday, 23 January 2023 at 08:49:50 UTC, Richard (Rikki) 
> Andrew Cattermole wrote:
>> ```d
>> Vector!int vector;
>> vector ~= 3;
>>
>> auto borrowed = vector[0];
>> func(borrowed);
>>
>> void func(scope ref int value) {
>> 	
>> }
>> ```
>>
>> Basically right now we're missing the lifetime checks 
>> surrounding borrowed & function parameter. Everything else is 
>> do-able right now, even if it isn't as cheap as it could be 
>> (like RC eliding).
>
> I'm writing a language with borrowing and ref counting (Neat), 
> and this is not a valid borrow. Basically you don't want to 
> take on borrowing with variables that are mutable by default, 
> because then you're asking for things like:
>
> ```d
> Vector!int vector;
> vector ~= 3;
> void evil() { vector = Vector!int.init; }
>
> auto borrowed = vector[0];
> func(borrowed);
>
> void func(scope ref int value) {
>   // destroy the last non-borrowed reference to vector, where 
> is your God now?
>   evil;
> ```

Addendum: This idiom is impossible in Rust because the equivalent 
of `void evil()` already captures `vector`. But nested functions 
are an essential part of D. This is why you cannot bolt borrowing 
onto a language that has been designed around a garbage 
collector; it needs support at every level. (Like variables being 
rvalue by default, cough.)


More information about the Digitalmars-d mailing list