Option.toRight returns 产品类型。这是什么意思?

Option.toRight returns Product type. What does it mean?

这是我之前 的后续。在评论中指出 toRight returns Product with Serializable 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]

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