Scala: why does a `for` comprehension on a Map sometimes yield a List?

后端 未结 1 1208
暖寄归人
暖寄归人 2021-01-04 03:23

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 on

相关标签:
1条回答
  • 2021-01-04 03:52

    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

    0 讨论(0)
提交回复
热议问题