Covariance & Contravariance

13
Covariance & Contravariance Jihun Lee (c) Mesh Korea

description

Covariance & Contravariance [email protected]

Transcript of Covariance & Contravariance

Page 1: Covariance &  Contravariance

Covariance & Contravariance

Jihun Lee (c) Mesh Korea

Page 2: Covariance &  Contravariance

Subtypingclass A {}

class B extends A {}

void ABC(A a);

!

A inst = new B() 👍

ABC(new B()) 👍

Page 3: Covariance &  Contravariance

Subtype of Array / Collection?

class A {}

class B extends A {}

void ABC(A a);

!

A[] arr = new B[10] 😦?

List<A> list = new List<B> 😦?

Page 4: Covariance &  Contravariance

Subtype of Array / Collection?

class A {}

class B extends A {}

void ABC(A a);

A[] arr = new B[10];

B inst1 = new B();

A inst2 = new A();

arr[0] = inst1;

arr[1] = inst2;

Runtime Error!

Page 5: Covariance &  Contravariance

Subtype of Array / Collection?

class A {}

class B extends A {}

void ABC(A a);

List<A> list = new List<B>();

B inst1 = new B();

A inst2 = new A();

list.add(inst1);

list.add(inst2);

Compile Error!

Page 6: Covariance &  Contravariance

Mistakes of Java / C#• Early version of Java & C# did not include generics

(parametric polymorphism)

• What about Array.sort(Object[] A)? If SomeClass[] is not a subtype of Object[], we cannot use sort method!

• Thus, they designed the language like that!!!

• JVM should check runtime type all the time, and programmers should be careful about sub typing of arrays

Page 7: Covariance &  Contravariance

Covariance & Contravariance

• Co-variance(vary together!): preserves the ordering, which orders types from more specific to more generic.

• Contra-variance(vary opposite): reverses the ordering!

• In-variance(not vary): neither of them.

Page 8: Covariance &  Contravariance

Covariance of Array• Let say Cat extends Animal

• Not every Animal[] is not Cat[]. There may be Dog in Animal[].

• Cat[] cats = new Animal[]: X ( not contravariant )

• Not every Cat[] can not be treated as Animal[]. Maybe you want to put Dog into Animal[].

• Animal[] animals = new Cat[]: X ( should not be covariant)

Page 9: Covariance &  Contravariance

Covariance of Array

• Read-only data types can be covariant.

• Write-only data types can be contravariant.

• Mutable data types should be invariant.

Page 10: Covariance &  Contravariance

Covariance of Array

• Array is covariant in Java, C#.

• Array is invariant in Scala.

Page 11: Covariance &  Contravariance

Covariance of Function types

• S1 → S2 ≤ T1 → T2 if T1 ≤ S1 and S2 ≤ T2

• “->” is contravariant for the input type and is covariant in the output type.

Page 12: Covariance &  Contravariance

Robustness principle

“Be conservative in what you do, be liberal in what you accept from others”

• Code that sends commands or data should conform completely to the specs.

• Code that receives input should accept non-conformance input when meaning is clear.

Page 13: Covariance &  Contravariance

Covariance of Generics• Declaration-site variance annotations ( C# )

interface IEnumerator<out T> { T Current { get; } bool MoveNext(); } !

• Use-site variance annotations ( Java )

<T extends Compaable<T>> T max(Collection<t> coll)