Scala for-comprehension with tuple decomposition

Pau*_*per 9 scala for-comprehension

for {
  a <- Some(1)
  b <- Some(2)
} yield (a, b)
Run Code Online (Sandbox Code Playgroud)

回报 Some((1, 2))

for {
  a <- Right(1).right
  b <- Left(2).left
} yield (a, b)
Run Code Online (Sandbox Code Playgroud)

回报 Left((1, 2))


现在我想在理解中分解元组.

for {
  (a, b) <- Some((1, 2))
  (c, d) <- Some((3, 4))
} yield (a, b, c, d)
Run Code Online (Sandbox Code Playgroud)

回报 Some((1, 2, 3, 4))

for {
  (a, b) <- Right((1, 2)).right
  (c, d) <- Left((3, 4)).left
} yield (a, b, c, d)
Run Code Online (Sandbox Code Playgroud)

无法编译:

error: constructor cannot be instantiated to expected type;
found   : (T1, T2)
required: scala.util.Either[Nothing,(Int, Int)]
                   (a, b) <- Right((1, 2)).right

error: constructor cannot be instantiated to expected type;
found   : (T1, T2)
required: scala.util.Either[(Int, Int),Nothing]
Run Code Online (Sandbox Code Playgroud)

为什么这个最后一个例子不起作用?有什么不同?

Pau*_*per 6

这是一个错误:

SI-5589:在生成器中使用Tuple2提取器对Either.RightProjection进行了解无法编译

withFilter()被称为(一些文档引用filter(),但在2.8中已经改变),这与类型推断混淆.

withFilter()用于类似的东西for(a <- b if c),虽然根据6.19它不应该在这种情况下使用.

后一个错误在SI-1336中被捕获:规范需要对for-comprehension进行类型检查以考虑可反复性,该问题已经开放了七年(2008年).

也许下一代会找到修复.


了解为什么必须在scala中的for循环中为模式匹配定义过滤器?