views:

1098

answers:

6

Can anyone explain me, the concept of covariance and contravariance in programming languages theory?

+4  A: 

Covariance is pretty simple and best thought of from the perspective of some collection class List. We can parameterize the List class with some type parameter T. That is, our list contains elements of type T for some T. List would be covariant if

S is a subtype of T iff List[S] is a subtype of List[T]

(Where I'm using the mathemtical definition iff to mean if and only if.)

That is, a List[Apple] is a List[Fruit]. If there is some routine which accepts a List[Fruit] as a parameter, and I have a List[Apple], then I can pass this in as a valid parameter.

def something(l: List[Fruit]) {
    l.add(new Pear())
}

If our collection class List is mutable, then covariance makes no sense because we might assume that our routine could add some other fruit (which was not an apple) as above. Hence we should only like immutable collection classes to be covariant!

oxbow_lakes
Good definition, but it misses the fact that not only types can be treated as co/contravariant. For example, Java `List<T>` isn't either, but Java wildcards let you treat in either covariant or contravariant fashion at point of use (rather than declaration) - of course, restricting the set of operations on the type to those that are actually covariant and contravariant for it.
Pavel Minaev
I believe that `List<? extends Fruit>` is a kind of *existential type*: i.e. `List[T] forSome T <: Fruit` - the *forSome T <: Fruit* is itself a type in this instance. Java is still not covariant in this type though. For example a method accepting a `List<? extends Fruit>` would not accept `List<? extends Fruit>`
oxbow_lakes
I mean "would not accept a `List<? extends Apple>` of course
oxbow_lakes
+10  A: 

Contravariance for the Rest of Us

Abhay
This is such a good explanation, not bookish. Thanks.
Heck - I even enjoyed reading it. More than Wikipedia's :)
xtofl
Well FWIW, the explanation is from a discussion that i and my colleagues had with an eminent member (former HP) when the discussion veered towards OOSC by Bertrand Meyers in which i think Meyers highlights the importance of contravariance in OOP. He said to us OOSC by B.Meyers is a must read book if one is to practically understand OOP.
Abhay
This maybe copyrighted material. This appears to be from a HP tech report and i think they have not given permission to republish. http://www.hpl.hp.com/techreports/90/HPL-90-121.pdf
chikak
Removed content (replaced with link). The original document is clearly marked with copyright (not that it needs to be...). Feel free to add your own original content in **addition** to the link. But don't paste verbatim.
Marc Gravell
This was a great read!
Hamish Grubijan
+6  A: 

Here are my articles on how we have added new variance features to C# 4.0. Start from the bottom.

http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx

Eric Lippert
OT: Eric, you had posted a puzzle on one of the questions earlier. I couldn't solve it and asked question about it. Could you please look at it? http://stackoverflow.com/questions/1167666/c-puzzle-reachable-goto-pointing-to-an-unreachable-label
SolutionYogi
OMG Eric Lippert!!!!
Janie
Watch it with those exclamation marks there. You can put someone's eye out with one of those things.
Eric Lippert
A: 

Bart De Smet has a great blog entry about covariance & contravariance here.

Mike Thompson