Want reasonable reference counting? Disable automatic sharing of immutable

Alexandru Ermicioi alexandru.ermicioi at gmail.com
Sun Nov 14 13:36:02 UTC 2021


On Sunday, 14 November 2021 at 07:16:43 UTC, Walter Bright wrote:
> On 11/12/2021 4:31 AM, Steven Schveighoffer wrote:
>> One of the prerequisites to doing reference counting is to 
>> have a mutable piece of data inside an immutable piece of data.
>
> Or maybe just give up on having immutable ref counted objects. 
> Ref counted objects are mutable.

That's best option imho. The ref counted objects should be 
mutable, while the payload, being of any constness level.

Like why do we even try to have ref counters be immutable? By 
their nature, they imply that some data is going to be modified 
(counter).
We could simply disable the ability to construct const and 
immutable versions of them, and that would remove the problem 
entirely.

For the case when a mutable ref counter becomes const, we could 
just disable access to the payload, forcing user to fix their 
code.
Or the counter could return a wrapper over payload that is not 
copyable or assignable, forcing user, to use wrapper with payload 
as soon as it was returned. Wrapper would have then an opDispatch 
overload, in order to not expose the payload to the user, and 
hence allow borrowing.

Best regards,
Alexandru.



More information about the Digitalmars-d mailing list