I have further reduced your example:
scala> if(!M.contains(1)) {M += 1 -> mutable.Set[Int](1)} else {M(1) += 1}; <console>:9: error: type arguments [Any] do not conform to trait Cloneable type parameter bounds [+A <: AnyRef] val res17 = ^
The problem occurs when the compiler tries to find a common return type for both branches: First -
scala> M += 1 -> mutable.Set[Int](1) res19: scala.collection.mutable.Map[Int,scala.collection.mutable.Set[Int]] = ...
And the "else" part
scala> M(1) += 1 res18: scala.collection.mutable.Set[Int] = Set(1)
If I add the return value to the end of this expression, REPL eats it without errors:
scala> if(!M.contains(1)) {M += 1 -> mutable.Set[Int](1)} else {M(1) += 1}; println("hello") hello
Because the type of the return expression is Unit.
source share