【发布时间】:2015-12-15 13:30:27
【问题描述】:
for {
a <- Some(1)
b <- Some(2)
} yield (a, b)
返回Some((1, 2))
for {
a <- Right(1).right
b <- Left(2).left
} yield (a, b)
返回Left((1, 2))
现在我想在 for 理解中分解元组。
for {
(a, b) <- Some((1, 2))
(c, d) <- Some((3, 4))
} yield (a, b, c, d)
返回Some((1, 2, 3, 4))
for {
(a, b) <- Right((1, 2)).right
(c, d) <- Left((3, 4)).left
} yield (a, b, c, d)
编译失败:
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]
为什么最后一个例子不起作用?有什么区别?
【问题讨论】:
-
报告了此问题的bug。