Persistent list

Andrei Alexandrescu via Digitalmars-d digitalmars-d at
Sat Nov 14 08:05:10 PST 2015

On 11/13/2015 07:28 PM, Jakob Ovrum wrote:
> On Friday, 13 November 2015 at 23:10:04 UTC, Andrei Alexandrescu wrote:
>> * Lines 11-12: I came to terms with the notion that some types cannot
>> be made immutable. We've been trying to do reference counting on
>> immutable objects for a long time. It's time to acknowledge that true
>> immutability (which the immutable keyword models) and reference
>> counting don't mix.
> External reference counting should work fine with immutable, i.e.
> RefCounted!(immutable T).

I've been asking this same question myself, vaguely, many times. 
Whenever I want to get work done using that mindset, I can't. Then I 
bring it up to Walter, and he asks again the same. Then I don't know how 
to explain it. Time to nip it in the bud once and for all. Clarify it 
forever so no need to bring it up again, ever.

The general pattern goes like this: "So okay, understood, immutable 
doesn't work with reference counting. Then, clearly what we need is an 
object with an immutable part and a mutable part! Instead of 
immutable(RC!T), we use RC!(immutable T) throughout. It has a mutable 
reference count and an immutable Problem solved!"

Technically, that clearly works. There's a problem with scaling it up:


Disallowing immutable(RC!T) in favor of RC!(immutable T) effectively 
disables composition of larger immutable objects from smaller ones.

One obvious thing to do is with a reference counted object it to make it 
a field of a larger object. That effectively makes that larger object 
impossible to be used with immutable, transitively.

That's not a bad thing; it's merely a factual acknowledgment that in the 
D programming language, immutable models (transitively and in a way that 
makes composition possible) true immutability of bits, which makes 
sharing possible at no cost. Reference counting is mutating, therefore 
it does not and cannot work with immutability as defined by D.

So disabling immutable(RC!T) in favor of RC!(immutable T) is a fine way 
to approach things, but not a solution to the general problem of making 
general reference counted D objects mutable. That problem does not have 
a solution.


More information about the Digitalmars-d mailing list