Want reasonable reference counting? Disable automatic sharing of immutable

Walter Bright newshound2 at digitalmars.com
Sun Nov 14 08:12:23 UTC 2021


On 11/13/2021 11:16 PM, Walter Bright wrote:
> Or maybe just give up on having immutable ref counted objects. Ref counted 
> objects are mutable.

Let me expand on that a bit.

This conversation reminds me of the old "logical const" debate, where people 
wanted to have constant objects that were not constant. That just would not work 
with the notion of transitive const.

Having a __mutable field in an immutable hierarchy is the same abomination. 
Attempting it immediately starts causing the rest of the type axioms in D to 
come apart. As Steven pointed out, sharing stops working.

It's like defining pi to be 3.0, and a right angle to be 89 degrees. Your house 
will not fit together if the corners are 89 degrees.

How does C++ manage it? They don't have immutable types. Const is just a 
documentation attribute.

How do functional languages do it? Reference counted types are not part of the 
type system. They are behind the curtain, just like how the gc works in D is 
behind the curtain, and associative arrays(!).

D has these choices:

1. ref counted objects are mutable

2. ref counted objects are outside of the type system

3. break the type system

I find (3) to be a bit ironic after D has been lambasted for having inconsistent 
semantic rules.

PS. I suspect that ref counted shared mutable objects are an indicator of insanity.


More information about the Digitalmars-d mailing list