15

If a class has a convariant type parameter such as Iterable[+A], is there any difference between declaring

def foo(bar: Iterable[_])

and

def foo(bar: Iterable[Any])

?


If a class has a contravariant type parameter such as Growable[-A], is there any difference between declaring

def foo(bar: Growable[_])

and

def foo(bar: Growable[Nothing])

?

Petr
  • 62,528
  • 13
  • 153
  • 317

1 Answers1

5

It does make a little difference when generic parameter is bounded. For example, if you had

class BoundedIterable[+A <: Something]
class BoundedGrowable[-A >: Something]

then type BoundedIterable[Any] and BoundedGrowable[Nothing] would be illegal.

I don't know if there is any other difference, but I can say for sure that you should prefer the wildcard-less variant wherever possible. That is because, actually, the very purpose of declaration-site type variance is to get rid of wildcards (which are a form of usage-site variance). When you say List[Any] you mean "list of anything", but when you say List[_] then you mean "list of we-don't-know-what". So the former is just way more clear, even though they may be equivalent in some particular case.

ghik
  • 10,706
  • 1
  • 37
  • 50
  • If a type parameter is bounded by `Something`, we could simply use `Something` in the place of `Any`/`Nothing`. In such a case, we could write `def foo(bar: MyBoundedType[Something])` (for both the covariant and contravariant case). – Petr Mar 09 '13 at 13:52
  • @Peter That's true. That's why I called it "a little" difference :) – ghik Mar 09 '13 at 13:58