这是我之前问题的后续。评论中指出,toRight回报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]
只是过于具体,因为编译器能够确定它是 a Productand Serializable,即使您不关心这些事情。这只是因为编译器总是告诉您它可以确定的最具体的类型,因为它自然不知道您想要什么级别的特异性。但它告诉你它是with scala.util.Either[String,Int],这就是你想要的,所以你不需要担心额外的东西。如果您想让类型更简单,那么可以,只需显式声明即可。