← Back to context

Comment by edflsafoiewq

7 years ago

> Immutability is stronger than C++ const correctness and easier to use at the same time.

C++-style const appears to be stronger than just immutability, since you can have immutable objects in C++, but you can also pass const references to mutable objects.

A const reference to a mutable object doesn't guarantee that the object won't change contrary to an immutable object. Hence const is weaker than immutable.

You can have immutable objects in C++, but C++ offers almost nothing to make dealing with such objects fast and easy. Also the lack of GC makes designing persistent data structures an order of magnitude harder task than in most other languages.

  • That's only if you do not use a library and/or have no idea how shared_ptr is implemented.

    • The standard library doesn't come with persistent collections included. Just the fact that they are not standard like in some other languages, causes fragmentation.

      As for shared_ptr, they are a good idea when you don't care about performance. And they don't solve cycles, which may appear in some structures (e.g. graphs).

That's because it is undefined behavior to cast a const object to a non-const object. Instead, you must tell the compiler via the mutable keyword or else it will make optimizations based on the assumption it can't change.

Precisely why it's harder to use. C++ is more expressive, and thus creating self-consistent designs is harder.

  • C++ is more expressive than what? Than C probably. Than Java/C# - arguable. Than Scala/Haskell/Rust/Python/Ruby/R - no way.

    • More expressive than Java, sure. Java doesn't have overloaded operators, or cv-qualifiers, or templates, or many many many other things that make the type design space more expressive.

      Ruby is hardly expressive at all in this respect - you can express to the interpreter very little about types, and the interpreter won't help you much at all.

      1 reply →

  • You'll have to give me an example for that. Having static types is more expressive than not having static types, but I think types make designs easier to make and understand. const is just another layer to the type system.

    • It's another aspect of type design you need to make decisions for. If you can't see that, I can't help you.