5

I'm reading the first section of "Scala in depth", there are two sentences in the first section about "covariance" and "contrvariance":

Covariance (+T or ? extends T) is when a type can be coerced down the inheritance hierarchy.

Contravariance(-T or ? super T) is when a type can be coerced up the inheritance hierarchy.

I have read some documents about "Covariance" and "Contravariance", but I can't understand the word "coerced down" and "coerced up" here in this context.

Community
  • 1
  • 1
Freewind
  • 193,756
  • 157
  • 432
  • 708
  • The first is called 'covariance' (not 'convariance'), so I have edited this – 0__ Dec 10 '13 at 15:47
  • @0__ Thank you, that's a hard word :) – Freewind Dec 10 '13 at 15:48
  • possible duplicate of [Scala covariance / contravariance question](http://stackoverflow.com/questions/663254/scala-covariance-contravariance-question) – dhg Dec 10 '13 at 16:31
  • @dhg I think the question is perhaps not "what is covariance/contravariance?" but rather "what has coercion got to do with it (and which type is being coerced, etc)?" – DNA Dec 10 '13 at 16:41

3 Answers3

10
[TOP / ABSTRACT]

Thing
  ↓
Animal
  ↓
Human
  ↓
Programmer
  ↓
Scala Dev

[BOTTOM / SPECIFIC]

Covariance: Accept T or lower.
I asked for a [+Human], I will accept any of these: [Human, Programmer, Scala Dev].

Contravariance: Accept T or higher.
I asked for a [-Human], I will accept any of these: [Thing, Animal, Human].

Inariance: Accept T and only T.

Coercion.
Coercing a type up/down the type hierarchy means checking that a type's super/sub type passes type constraints. For example, a covariant function needs a Human but we've only got a Programmer, that's ok, the compiler can coerce the Programmer into Human to satisfy typing constraints.

Golly
  • 1,319
  • 8
  • 18
  • Great answer. The only question is in the case of "For example, a covariant function needs a Human but we've only got a Programmer, that's ok, the compiler can coerce the Programmer into Human to satisfy typing constraints.", we should call it "coerce up" or "coerce down"? – Freewind Dec 11 '13 at 13:34
  • 2
    Coercing a Programmer into a Human would be "coercing up". Hey I just realised that's the opposite of what the excerpt you pasted says! Hmmm, I'd say the author made a mistake. – Golly Dec 11 '13 at 23:45
1

In this case coerced means the compiler can treat the type as a type further up/down the inheritance hierarchy.

Think of it as upcasting or downcasting except the compiler is doing it automatically, so it is not a cast (which could suggest that explicit code was required to perform it).

iain
  • 10,798
  • 3
  • 37
  • 41
1

This response is taken from lectures given by Martin Odersky (the creator of Scala) on Coursera. We note:

S<:T means: S is a subtype of T, and
S>:T means: S is a supertype of T, or T is a subtype of S.

Say C[T] is a parameterized type and A, B are types such that A<:B. In general there is three possible relationships between C[A] and C[B]:

C[A]<:C[B] ---------> C is covariant
C[A]>:C[B] ---------> C is contravariant
neither C[A] nor C[B] is a subtype of the other ---------> C is nonvariant

Scala lets you declare the variance of a type by annotating the type parameter:

class C[+A] { ... }  ---------> C is covariant
class C[-A] { ... } ----------> C is contravariant
class C[A]  { ... } ----------> C is nonvariant

Hope this might help!

Tom
  • 418
  • 4
  • 14
Momog
  • 567
  • 7
  • 27