15

Why, in the below code example, does isAList's for comprehension yield a List, but the other two yield Maps? I can't think of any reason - the only difference seems to be that there is isAList's comprehension declares two variables, and the others declare one or zero.

object Weird {
  def theMap: Map[Int, String] =
    Map(1 -> "uno", 2 -> "dos", 3 -> "tres")

  def main(args: Array[String]) {

    val isAMap = for {
      (key, value) <- theMap
    } yield (key*2 -> value*2)

    val isAlsoAMap = for {
      (key, value) <- theMap
      doubleKey = key*2
    } yield (doubleKey -> value*2)

    val isAList = for {
      (key, value) <- theMap    
      doubleKey = key*2
      doubleValue = value*2
    } yield (doubleKey -> doubleValue)

    println(isAMap)
    println(isAlsoAMap)
    println(isAList)
  }
}

outputs

Map(2 -> unouno, 4 -> dosdos, 6 -> trestres)
Map(2 -> unouno, 4 -> dosdos, 6 -> trestres)
List((2,unouno), (4,dosdos), (6,trestres))

I am comparatively new to Scala, so apologies if I'm being incredibly naive about something!

2
  • 1
    Look at the output of scalac -print to see the de-sugared program.
    – Paul
    Commented Jun 29, 2013 at 6:56
  • @Paul: I tried that just now, and although the output looks quite interesting, I'm not sure how to interpret it…
    – S M
    Commented Jun 29, 2013 at 7:05

1 Answer 1

19

Recently discussed on the ML:

https://groups.google.com/forum/#!msg/scala-internals/Cmh0Co9xcMs/D-jr9ULOUIsJ

https://issues.scala-lang.org/browse/SI-7515

Suggested workaround is to use a tuple to propagate the variables.

scala> for ((k,v) <- theMap; (dk,dv) = (k*2,v*2)) yield (dk,dv)
res8: scala.collection.immutable.Map[Int,String] = Map(2 -> unouno, 4 -> dosdos, 6 -> trestres)

More on the tupling mechanism:

What are the scoping rules for vals in Scala for-comprehensions

1
  • Thanks for the prompt answer! That mailing list answer makes a lot of sense.
    – S M
    Commented Jun 30, 2013 at 1:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.