scala - Option.toRight 返回产品类型。这是什么意思?

标签 scala either

这是我之前的 question 的后续内容。评论中指出,toRight 返回 Product with Serialized with scala.util.Either:

scala> val ox = Some(0)
ox: Some[Int] = Some(0)

scala> ox.toRight("No number")
res0: Product with Serializable with scala.util.Either[String,Int] = Right(0)

现在我想知道它与我需要的 Either 类型有何不同。我应该像这样显式添加 Either[String,Int] 吗?

scala> ox.toRight("No number"): Either[String, Int]
res1: Either[String,Int] = Right(0)

最佳答案

类型

Product with Serializable with scala.util.Either[String,Int]

只是过于具体,因为编译器能够确定它是一个 ProductSerialized,即使您不关心这些事情。这只是因为编译器总是告诉您它可以确定的最具体的类型,因为它自然不知道您想要什么级别的特异性。但它告诉你它是 with scala.util.Either[String,Int],这就是你想要的,所以你不需要担心额外的东西。如果您想让类型更简单,那么可以,只需显式声明即可。

关于scala - Option.toRight 返回产品类型。这是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34715462/

相关文章:

scala - 如何在 Play/Scala 中声明名为 'type' 的变量?

c - 使用 union 处理错误

scala - `contains[BB >: B](e: BB): Boolean` 而不是 `contains(e: Any): Boolean` 的好处

Haskell 返回 Either Double Bool

scala - apache flink : how to interpret DataStream. 打印输出?

java - 部署多模块Play项目

scala - 没有案例类的模式匹配

scala - 映射存在类型列表

scala - Scala错误处理中的错误上下文

scala - 过滤一组Either[x, y],记录