Scala: Why does Seq.contains take an Any argument, instead of an argument of the sequence type?

Lots of interesting answers, but here’s my own theory: if contains did not receive an Any, then Seq could not be co-variant.

See, for instance, Set, which is not co-variant and whose contains take an A instead of an Any.

The reasons for that is left as an exercise to the reader. 😉 But here is a hint:

scala> class Container[+A](elements: A*) {                         
     |   def contains(what: A): Boolean = elements exists (what ==)
     | }
<console>:7: error: covariant type A occurs in contravariant position in type A of value what
         def contains(what: A): Boolean = elements exists (what ==)
                      ^

Leave a Comment