Question

I'm really only familiar with how to do this in javascript so trying it in Scala has presented some problems.

Anyways I have a function that I will be calling many times. Each time it's called I want to update some sort of associative array (what in javascript would be an object) with a new key pair value or update an existing one.

In JS it looks something like this...

var obj = {}
// callFunction()
obj = {"a": {"len": 1, "n": 34 }} 
// callFunction()
obj = {"a": {"len": 1, "n": 34 }, "b": {"len": 1, "n": 25 }}
// callFunction()
obj =  {"a": {"len": 2, "n": 34 }, "b": {"len": 1, "n": 25 }}

I want to emulate that in scala and I'm struggling with the best or right way to do it. I've tried something like

val mutMap3 = collection.mutable.Map.empty[String, collection.mutable.Map.empty[String, Int]]
Was it helpful?

Solution

val mutMap3 = collection.mutable.Map.empty[String, collection.mutable.Map[String, Int]]

Or, better:

import collection.mutable.{ Map => MMap }
val mutMap3 = MMap.empty[String, MMap[String, Int]]

It's complaining because you have "empty" inside the type, but empty is a method, not a type. What you want is for this to say that it's an empty Map whose values must be Maps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top