Is there any \"good\" code pattern for a Class
initializing and populating private mutable Maps, and then exposing them as immutable ones? Or should I just eter
I think using var
s of immutables rather that val
s of mutables, evolving the var
collections according to my initialization logic, can be the optimal pattern wherever applicable. No duplicate collections, no code to convert from immutable to mutable, clear type definitions at the top of the Class...
However, it is my understanding that this functional way trades-off with run time efficiency, as mutable collections can provide better modification performance when running modification logic on them while building them.
Something like:
scala> class X {
| private val mb = collection.immutable.Map.newBuilder[String, Int]
| def m = mb.result
| mb += ("a" -> 1) // stuff
| }
defined class X
scala> new X().m
res0: scala.collection.immutable.Map[String,Int] = Map(a -> 1)