Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "contains" implemented in Seq, but not in Iterable or Traversable? [duplicate]

I would like to call 'contains' on my Iterables :-)

like image 469
adam77 Avatar asked Nov 19 '22 11:11

adam77


1 Answers

The reason Iterable does not have a contains method is because the way it is defined can have direct consequences on variance. Basically, there are two type signatures that make sense for it:

def contains(v: Any): Boolean
def contains(v: A): Boolean

The second definition has increased type safety. However, A, which is the type parameter of collection, appears in a contra-variant position, which forces the collection to be invariant. It could be defined like this:

def contains[B >: A](v: B): Boolean

but that wouldn't offer any improvement over the first signature, using Any.

As a consequence of this, you'll see that immutable.Seq is co-variant and uses the first signature, while immutable.Set is invariant and uses the second signature.

like image 174
Daniel C. Sobral Avatar answered Dec 05 '22 12:12

Daniel C. Sobral