Object immutability

Hi @pcasafont,
your proposal is interesting, but I am really sceptic about it.

But there’s a catch in here: with only that (…), we cannot assert that a immutable object will not be modified in another thread that has a mutable reference to it. In my opinion that’s not a good enough reason, but we could still do something about it in runtime.

I agree with you, for this reason you should not mix up immutability with read-only.

Every object could have an atomic flag stack

You need an atomic counter?

When an object becomes referenced as immutable, that flag would be pushed, and when leaving that reference’s scope it would be popped.

Two atomic operations for each variable?

This could be optimized to have pretty much no overhead

How it is possible optimize atomic counters without disabling it?

What do you think about it?

C++ const keyword is an unfortunate feature, API design become harder and read-only doesn’t mean immutable.
Immutable data class looks as safer choice.

1 Like