Did languages before Rust have inherited mutability?

Inherited mutability has some big advantages, because it makes code:

  • simpler to write, because it prevents some classes of concurrency bugs.
  • easier to understand, e.g. regarding what a function is doing with its parameters.
  • more optimizable.

Despite this, languages like C and Javascript only have shallow constants (I think.) Are there any languages before Rust that have this kind of mutability guarantee?

I'm not sure I'm understanding the term "inherited mutability" correctly. From what I could find searching the Internet, it seems to mean that fields of a struct inherit the mutability of the struct that they are a part of, but that each field may still override this inherited property.

If that interpretation is correct, then I'd say that C++ definitely has inherited mutability, nothwithstanding a different "default mutability".

class Foo {
    /// `inherited` is const if `Foo` is const, otherwise non-const.
    int inherited;

    /// `overridden_const` is always const, no matter `Foo`'s status.
    const int overridden_const;

    /// `overridden_mut` is always non-const, no matter `Foo`'s status.
    mutable int overridden_mut;

I think this feature does not translate to C, where structs are really just grab-bags of fields.


Depends how strongly you interpret "guarantee". C++ definitely has (2), as you can define a parameter as vector<int> const& to communicate that you're only planning on reading from it, and the vector class itself doesn't offer mutation of its internals when it's const itself.

But C++ has const_cast, which means that (3) is basically impossible in general. And it doesn't have borrowck, so you can have multiple mutable T& to the same thing, so it doesn't help with (1).

1 Like

Something something something "ST monad" something something.

That said, C++ gets closer than any other language I can think of; it's the only language I know that has a concept of immutability built into the language that even vaguely resembles that of rust.

In many languages, the only way you can have a immutable value is by defining a new type that has no public API for mutation. I mean, that's basically the sole reason why Python's tuple exists. JavaScript/Java have keywords called const/final, respectively, though I prefer to call them a joke. (they prevent you from reassigning the binding and... uh, that's it)

1 Like

Ada has the constant keyword which guarantees that records do not change during their lifetime.

Also many functional languages are inherently immutable (no side-effects) and changing values is only possible by creating new objects.