Search code examples
scalastandard-libraryeither

Benefit of `contains[BB >: B](e: BB): Boolean` instead of `contains(e: Any): Boolean`


What's the benefit of defining contains as contains[BB >: B](e: BB): Boolean instead of contains(e: Any): Boolean in Scala.

Either.contains in the Scala standard library uses the first signature and I am unclear on the benefit of such a signature over the second signature.


Solution

  • In this case, none: the two signatures are equivalent. But it provides consistency with signatures of other members such as

     getOrElse[BB >: B](or: ⇒ BB): BB
    

    For them, there is a difference because BB is part of the return type, so if we pass a B, we get B back, which we wouldn't have with getOrElse(or: => Any): Any.

    What boggles me is why you would ever want to pass something that's not a B to that function

    You wouldn't. But contains(e: B) would not allow Either to be covariant. Try it, the compiler will reject it on the grounds that B appears in a contravariant position. Covariance means that e.g. Either[A, SubtypeOfFoo] is a subtype of Either[A, Foo]. So any call which is legal for Either[A, Foo] must be also legal for Either[A, SubtypeOfFoo], and this includes contains(Foo).