I have a function like this:
private def add[T](n: String, t: T, k: Map[String,T]): T = { k += (n -> t); t }
the compiler complains that there is a reassignment to val, so short of changing this to a mutable map, is there a way to say something like “var k: Map…” as in a case class?
Thanks!
What you’re asking for is a pass by reference argument. The JVM doesn’t have those and neither does Scala.*
You will either have to return the updated map:
or return both, or if you must return
T, write a wrapper class:*Well, not directly. Of course, one must change vars in an outer context somehow with closures, and in fact what Scala does is use a wrapper class much like the one I show above.