gpt4 book ai didi

Scala 的要么元组为 Right

转载 作者:行者123 更新时间:2023-12-04 14:58:35 26 4
gpt4 key购买 nike

假设我有以下代码:

val either: Either[String, (Int, Int)] = Right((1,2))
for {
(a, b) <- either.right
} yield a + b

当我在 REPL 中评估它时,我得到

:13: error: constructor cannot be instantiated to expected type; found : (T1, T2) required: scala.util.Either[Nothing,(Double, Double)] (a, b) <- a.right ^ :14: error: not found: value a } yield a + b ^



为什么我有这样的错误?我不能对来自“Either's Right”的元组进行模式匹配吗?

最佳答案

问题似乎是 Scala 错误 https://issues.scala-lang.org/browse/SI-7222 .将 for comprehension 转换为 flatMap/map 符号似乎有效。

val either: Either[String, (Int, Int)] = Right((1, 2))
either.right.map {
case (a, b) =>
a + b
}

either: Either[String,(Int, Int)] = Right((1,2))
res0: Serializable with Product with scala.util.Either[String,Int] = Right(3)

关于Scala 的要么元组为 Right,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24066493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com