4

I wrote a function very similar to this:

def writeMyEl (x: TypeA, y: TypeB, z : TypeC) {
  if (myMutableHashMap.contains((x, y)))
    myMutableHashMap(x, y) = z else
      myMutableHashMap += (x, y) -> z
}

In real code Types A and B are enumerations and C is a case class. myMutableHashMap is defined as a val of type scala.collection.mutable.HashMap[(TypeA, TypeB), TypeC] inside the same class as the writeMyEl function.

The Scala (2.8) compiler says:

error: too many arguments for method update: (key: (TypeA, TypeB),value: TypeC)Unit

What am I doing wrong?

1 Answer 1

11

Try myMutableHashMap((x, y)) = z. In fact, you don't need the check, since the documentation for += says "Adds a new key/value pair to this map. If the map already contains a mapping for the key, it will be overridden by the new value." So your function can just be written as

def writeMyEl (x: TypeA, y: TypeB, z : TypeC) {
  myMutableHashMap += (x, y) -> z
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.