Michael
Michael

Reputation: 42070

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

This is a follow-up to my previous question. It was noted in the comments that the 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)

Now I wonder how it differs from the Either type I need. Should I add Either[String,Int] explicitly like that ?

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

Upvotes: 3

Views: 1987

Answers (1)

dhg
dhg

Reputation: 52691

The type

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

is just overly-specific because the compiler is able to determine that it's a Product and Serializable, even though you don't care about those things. This is just because the compiler always tells you the most specific type it can determine since, naturally, it can't know what level of specificity you want. But it's telling you that it is with scala.util.Either[String,Int], which is what you want, so you don't need to worry about the extra stuff. If you want to make the type simpler, then yes, just declare it explicitly.

Upvotes: 6

Related Questions