Skip to content

Issue with multiple mid-stream variables in for comprehensions #16812

Open
@ftucky

Description

@ftucky

Compiler version

3.2.0

Minimized code

  for k -> v <- Map(0->"A")
  dummy = 0
  yield k->v
// Result is a Map[Int,String] (expected)


  for 
    k -> v <- Map(0->"A")
    dummy  = 0
    dummy2 = 2
  yield k->v
// Result is a List[(Int,String)] (*UNEXPECTED*)

  for 
    k -> v <- Map(0->"A")
    dummy  = 0
    u <- 1::Nil 
    dummy2 = 2
  yield k->v
// Result is a Map[Int,String]  (expected)

Output

Map(0 -> A)
List((0,A))
Map(0 -> A)

Expectation

Map(0->A)
Map(0->A)
Map(0->A)

The presence of two successive midstream variable bindings seems to unsettle the compiler.

The type of the for expression is expected to be the one of the first (outer) container, not a List as in the second example.

Note that when the two bindings are separated by a Generator (as in the 3rd example) the expected behavior is restored.

Same observation in scala2 (2.11.2) :

for (u <- Map(0->"A");v=1) yield u
// scala.collection.immutable.Map[Int,String] = Map(0 -> A)   // (expected)

for (u <- Map(0->"A");v=1;w=2) yield u
// scala.collection.immutable.Iterable[(Int, String)] = List((0,A)) // (*UNEXPECTED*)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:desugarDesugaring happens after parsing but before typing, see desugar.scalaitype:bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions