这是我之前的 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]
只是过于具体,因为编译器能够确定它是一个 Product
和 Serialized
,即使您不关心这些事情。这只是因为编译器总是告诉您它可以确定的最具体的类型,因为它自然不知道您想要什么级别的特异性。但它告诉你它是 with scala.util.Either[String,Int]
,这就是你想要的,所以你不需要担心额外的东西。如果您想让类型更简单,那么可以,只需显式声明即可。
关于scala - Option.toRight 返回产品类型。这是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34715462/