Martin Odersky gave the Keynote talk for Scala Days 2013.
One slide was titled "When is an Object Mutable?" Its contents read as follow:
class Memo[T, U](fn: T => U) {
val memo = new mutable.WeakHashMap[T, U]
def apply(x: T) = memo.getOrElseUpdate(x, fn(x))
}
// an object is mutable if its (functional) behavior depends on its history
new Memo {i: Int => i + 1} // immutable
var ctr = 0;
new Memo { i: Int => ctr += i; ctr } // mutable
Please explain why the Memo examples are, respectively, immutable and mutable.