Allocator-aware @safe reference counting is still not possible

Dukc ajieskola at gmail.com
Tue Jan 24 14:39:45 UTC 2023


On Monday, 23 January 2023 at 17:49:20 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
> 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 :)

I'm afraid it's more complicated than you think.

`thing` might have its destructor called before the end of `got` 
lifetime. The language could pretty trivially prevent doing that 
directly, but what if you have a `scope` pointer to `thing` and 
call the destructor via it? or a `scope SumType!(Thing*, 
int*)[5]` variable, that may contain both references to both 
`thing`s and ints?

These are probably solvable, but the solution is going to be at 
least as complex as `@live`, if not more so.


More information about the Digitalmars-d mailing list