Posts Scala tip: getOrElseUpdate
Post
Cancel

Scala tip: getOrElseUpdate

I found myself recently looking at the following code which I wrote when I was getting started with Scala:

1
2
3
4
5
6
7
8
def getModelState(modelPrefixedId: String) =
  modelStates.get(modelPrefixedId) match {
    case Some(modelState) => modelState
    case None =>
      val modelState = new ModelState(modelPrefixedId)
      modelStates += modelPrefixedId -> modelState
      modelState
  }

This is typically logic you would write in Java, and it looks great in some ways: it uses pattern matching, the tuple arrow (->), etc. But it turns out that Scala collections already provide the getOrElseUpdate method on mutable maps. The 8 lines above translate simply into:

1
2
def getModelState(modelPrefixedId: String) =
  modelStates.getOrElseUpdate(modelPrefixedId, new ModelState(modelPrefixedId))

Morality: know your collections!