2 ms·
const is one of the reasons why I prefer strongly-typed languages based on reference types. I want to love the no-overhead policy of C++, but value types make c
by gurkendoktor 9y ago
const is one of the reasons why I prefer strongly-typed languages based on reference types. I want to love the no-overhead policy of C++, but value types make const annoying (e.g. no vector<const string>) and require complicated rvalue-reference mechanics to be efficient when working with nested data structures.
In Java, where everything is an (expensive) reference type, building an immutable LabeledPoint2D class is a no-brainer. "final String label; final double x, y;" - done. I find this much easier to reason about than the C++ equivalent where the const-ness only emerges from how you use the class.
Add Objective-C's system where Array<T>, Set<T>, String etc. are all immutable, and only their derived types (MutableArray<T>, MutableSet<T>, MutableString) are mutable, and you get a reasonably effective and efficient implementation of tail-constness without extending your type system.
It's bizarre how C++ and D both try so hard to implement const, yet higher-level languages that could easily have better const support don't really bother (think Java, where you never know which List<T> is mutable).