0

I am trying to use the foreach method on scala.collection.immutable.Map. But no matter how I change my format, I receive a compilation error. I have referenced the question: Scala Map foreach, but it still doesn't work:

val m = Map[String, Int]("a" -> 1, "b" -> 2, "c" -> 3)
m.foreach(p => println(">>> key=" + p._1 + ", value=" + p._2))

or:

val m = Map[String, Int]("a" -> 1, "b" -> 2, "c" -> 3)
m.foreach((e: (String, Int)) => println(e._1 + "=" + e._2))

both report:

type mismatch; found : Unit required: String

My Scala library version is 2.10.4 and my eclipse version is Release 4.3.2.

Community
  • 1
  • 1
wuchang
  • 3,003
  • 8
  • 42
  • 66
  • 10
    it appears that it believes that the result of this entire computation should be String based on some surrounding context that you haven't shown us, however the type of foreach (and thus your entire expression) is Unit – stew Apr 02 '15 at 01:45
  • Yah, we need more. The lines provided work – Justin Pihony Apr 02 '15 at 02:08
  • You might have intented to use `map` followed by `mkString` to convert your `Map` into `String`. In that case remove `println`. – yǝsʞǝla Apr 02 '15 at 03:03

0 Answers0