3 ms·
As an example in Scala, consider: val buf = scala.collection.mutable.ArrayBuffer.empty[Int] buf += 1 is perfectly valid. You can't reassign buf to another
by SatvikBeri 8y ago
As an example in Scala, consider:
val buf = scala.collection.mutable.ArrayBuffer.empty[Int]
buf += 1
is perfectly valid. You can't reassign buf to another object, but you can change the underlying object. Making something "truly" immutable when that's allowed is tricky. Apache Spark (built on top of Scala) goes a long way to try and achieve this, and is able to do a number of optimizations as a result, but IIRC there are still significant cases it isn't able to resolve.
- qwerty456127 8y agoI know this (and even use this occasionally), that's why there are whole separate families of collections in Scala - mutable and immutable collections. And I think it is possible for the compiler to figure out if a type (e.g. a collection type) is mutable or not given its source code and even if it isn't it probably can be made possible by introducing some sort of special annotations to class definitions.