Allocator-aware @safe reference counting is still not possible

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Mon Jan 23 17:49:20 UTC 2023


On 24/01/2023 6:32 AM, Paul Backus wrote:
> On Monday, 23 January 2023 at 16:59:16 UTC, Richard (Rikki) Andrew 
> Cattermole wrote:
>> On 24/01/2023 5:39 AM, Paul Backus wrote:
>>> Have you seen the borrow method [1] used by SafeRefCounted? It is 
>>> already possible, in the current D language, to prevent a container 
>>> or smart pointer from leaking references. The syntax is awkward, 
>>> because you have to use a callback, but it can be done.
>>
>> Yes I'm aware of this method. I've talked about it with Robert Schadek 
>> during last DConf Online who argued that this is the only way forward. 
>> This is not the way forward as far as I'm concerned. It is a major 
>> step backwards in usability as it is not how people work with arrays 
>> or data types in general.
> 
> I agree that it's bad UX, but what's the alternative? Implement a borrow 
> checker in D? It'll take 5-10 years and won't even work properly when 
> it's done. At that point, you may as well just tell people to switch to 
> Rust.

A lot of it is already done with DIP1000.

When you call a function and pass a borrowed value in, that part is 
complete.

What we are missing is the initial borrow action and guaranteeing it 
being tied to another object in terms of life.

This only really needs to cover within a function body, so the DFA here 
should actually be really easy.

Perhaps something like this (note ref not actually required for pointer 
types):

```d
struct Thing(T) {
	ref T get() scope { ... }
}

{
	Thing thing;
	scope ref got = thing.get; // owner = thing
	func(got); // ok parameter is scope

	thing = Thing.init;// Error: thing must out live got variable, but 
thing is being assigned to.
	return got; // Error: scope variable thing must out live got variable, 
but got is being returned.
}

void func(scope ref T value) {}
```

For the rest, I'm glad that we are converging on a possible position :)



More information about the Digitalmars-d mailing list