Covariance & Contravariance
-
Upload
mesh-korea -
Category
Technology
-
view
128 -
download
1
description
Transcript of Covariance & Contravariance
![Page 1: Covariance & Contravariance](https://reader035.fdocuments.in/reader035/viewer/2022080209/546f05cfb4af9ff50b8b4596/html5/thumbnails/1.jpg)
Covariance & Contravariance
Jihun Lee (c) Mesh Korea
![Page 2: Covariance & Contravariance](https://reader035.fdocuments.in/reader035/viewer/2022080209/546f05cfb4af9ff50b8b4596/html5/thumbnails/2.jpg)
Subtypingclass A {}
class B extends A {}
void ABC(A a);
!
A inst = new B() 👍
ABC(new B()) 👍
![Page 3: Covariance & Contravariance](https://reader035.fdocuments.in/reader035/viewer/2022080209/546f05cfb4af9ff50b8b4596/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022080209/546f05cfb4af9ff50b8b4596/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022080209/546f05cfb4af9ff50b8b4596/html5/thumbnails/5.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022080209/546f05cfb4af9ff50b8b4596/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022080209/546f05cfb4af9ff50b8b4596/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022080209/546f05cfb4af9ff50b8b4596/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022080209/546f05cfb4af9ff50b8b4596/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022080209/546f05cfb4af9ff50b8b4596/html5/thumbnails/10.jpg)
Covariance of Array
• Array is covariant in Java, C#.
• Array is invariant in Scala.
![Page 11: Covariance & Contravariance](https://reader035.fdocuments.in/reader035/viewer/2022080209/546f05cfb4af9ff50b8b4596/html5/thumbnails/11.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022080209/546f05cfb4af9ff50b8b4596/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022080209/546f05cfb4af9ff50b8b4596/html5/thumbnails/13.jpg)
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)