50
Covariance and Contravariance. Say what?!

Covariance and contravariance. Say what?! (Agile Talks #22)

Embed Size (px)

Citation preview

Page 1: Covariance and contravariance. Say what?! (Agile Talks #22)

Covariance andContravariance.

Say what?!

Page 2: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 3: Covariance and contravariance. Say what?! (Agile Talks #22)

Alin Pandichi

Page 4: Covariance and contravariance. Say what?! (Agile Talks #22)

Alin Pandichi

Open spaceCoding DojoBucharest Java User GroupGlobal Day of Coderetreat

Page 5: Covariance and contravariance. Say what?! (Agile Talks #22)

Alin Pandichi

Open spaceCoding DojoBucharest Java User GroupGlobal Day of Coderetreat

Software developer @Mozaic LabsBuilding eventriX

Page 6: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 7: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 8: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 9: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 10: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 11: Covariance and contravariance. Say what?! (Agile Talks #22)

Covariance = the property of a function of retaining its form when the variables 

are linearly transformed.

Page 12: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 13: Covariance and contravariance. Say what?! (Agile Talks #22)

Covariance = changing the input moves the function in the same direction.

Page 14: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 15: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 16: Covariance and contravariance. Say what?! (Agile Talks #22)

Contravariance = The basis and the vector transform in an opposite way.

Page 17: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 18: Covariance and contravariance. Say what?! (Agile Talks #22)

Contravariance = changing the input moves the function in the opposite direction.

Page 19: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 20: Covariance and contravariance. Say what?! (Agile Talks #22)

Cov

aria

nce

Page 21: Covariance and contravariance. Say what?! (Agile Talks #22)

Contravariance

Cov

aria

nce

Page 22: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 23: Covariance and contravariance. Say what?! (Agile Talks #22)

Variance refers to how subtyping between complex types (lists, arrays) relates to 

subtyping between their components (individual items).

Depending on variance, the subtyping relation may be either preserved, reversed, or ignored.

Page 24: Covariance and contravariance. Say what?! (Agile Talks #22)

Integer is a subtype of  Number Integer i = 1;Number n = i;

Number  <  Integer Number  ← Integer

< means “is a subtype of”← means “can be assigned to”*read from right to the left

Page 25: Covariance and contravariance. Say what?! (Agile Talks #22)

Integer[] is a subtype of  Number[]  Integer[] ints = {1, 2, 3};Number[] nums = ints;

Number[]  <  Integer[] Number[]    ← Integer[]

< means “is a subtype of”← means “can be assigned to”*read from right to the left

Page 26: Covariance and contravariance. Say what?! (Agile Talks #22)

Number     ←  Integer  Number[]  ←  Integer[] 

A   B←  means “B can be assigned to A”

Array types can be assigned in the same order.Arrays are covariant.

Page 27: Covariance and contravariance. Say what?! (Agile Talks #22)

ArrayList<Integer> ints = new ArrayList<Integer>();ArrayList<? extends Number> nums = ints;

Number   ←  IntegerNumber[]    ← Integer[]

<? extends Number>   ←  <Integer>  

Can be assigned in the same order.This generic list is covariant.

A   B←  means “B can be assigned to A”

Page 28: Covariance and contravariance. Say what?! (Agile Talks #22)

ArrayList<Object> objs = new ArrayList<Object>();ArrayList<? super Number> nums = objs;

Object      ←  NumberObject[]       ← Number[]<Object>     → <? super Number>  

Can be assigned in the reverse order.This generic list is contravariant.

A   B←  means “B can be assigned to A”

Page 29: Covariance and contravariance. Say what?! (Agile Talks #22)

Interesting properties of covariance and contravariance:

With covariance you can only take values out.

With contravariance you can only put values in.

Page 30: Covariance and contravariance. Say what?! (Agile Talks #22)

With covariance you can only take values out.

ArrayList<? extends Error> errs = new ArrayList<StackOverflowError>();

Error e = errs.get(0);

errs.add(new Error("")); // compiler errorerrs.add(new StackOverflowError("")); // compiler error

Page 31: Covariance and contravariance. Say what?! (Agile Talks #22)

With contravariance you can only put values in.

ArrayList<? super StackOverflowError> errs = new ArrayList<Error>();

errs.add(new StackOverflowError("w00t"));

Error e = errs.get(0); //compiler-error

StackOverflowError soe = errs.get(0); //compiler-error

Page 32: Covariance and contravariance. Say what?! (Agile Talks #22)

public void copy(List<? extends Number> source,List<? super Number> destination) {

for(Number number : source) {destination.add(number);

}}

List<Integer> myInts = asList(1,2,3,4);List<Integer> myDoubles = asList(3.14, 6.28);List<Object> myObjs = new ArrayList<Object>();copy(myInts, myObjs);copy(myDoubles, myObjs);

Page 33: Covariance and contravariance. Say what?! (Agile Talks #22)

In C#, covariant generic interfaces are annotated with out:

interface IMessageRecieved <out T>{

T RecievedData();}

Covariance ­ take values out.

Page 34: Covariance and contravariance. Say what?! (Agile Talks #22)

In C#, contravariant generic interfaces are annotated with in:

interface IMessageSend <in T>{

void SendData(T data);}

Contravariance – put values in.

Page 35: Covariance and contravariance. Say what?! (Agile Talks #22)

Covariance

Enables you to use a more derived type than originally specified.

You can assign an instance of IEnumerable<Derived> to a variable of type IEnumerable<Base>.

Page 36: Covariance and contravariance. Say what?! (Agile Talks #22)

Contravariance

Enables you to use a more generic (less derived) type than originally specified.

You can assign an instance of IEnumerable<Base> to a variable of type IEnumerable<Derived>.

Page 37: Covariance and contravariance. Say what?! (Agile Talks #22)

class A { Object getSomething(){}}

class B extends A { String getSomething() {}}

Covariance because it returns a String  which extends Object (more derived)

Page 38: Covariance and contravariance. Say what?! (Agile Talks #22)

class A { void doSomething(String param){}}

class B extends A { void doSomething(Object param){}}

Contravariance because it takes an Object which is a super class of String (less derived)

Page 39: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 40: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 41: Covariance and contravariance. Say what?! (Agile Talks #22)

Type systems that support subtyping.

Page 42: Covariance and contravariance. Say what?! (Agile Talks #22)

Programming language designers devising typing rules for arrays, inheritance, and 

generic datatypes

Page 43: Covariance and contravariance. Say what?! (Agile Talks #22)

Programmers who use inheritance and override methods.

Page 44: Covariance and contravariance. Say what?! (Agile Talks #22)

Programmers who develop generic interfaces, methods and code.

Page 45: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 46: Covariance and contravariance. Say what?! (Agile Talks #22)

Subtyping relation

Data access

Overriding methods

Wildcard generics

Covariance

Keep the order

Take values 

out

More derived return type

extends

Contravariance

Reverse the order

Put values in

Less derived 

parameter type

super

Page 47: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 48: Covariance and contravariance. Say what?! (Agile Talks #22)
Page 49: Covariance and contravariance. Say what?! (Agile Talks #22)

Resources

http://www.i­programmer.info/programming/theory/1632­covariance­and­contravariance­a­simple­guide.htmlhttps://dzone.com/articles/covariance­and­contravariancehttps://msdn.microsoft.com/en­us/library/dd799517(v=vs.110).aspxhttps://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Covariant_arrays_in_Java_and_C.23https://www.codeproject.com/articles/86419/variance­in­c­net­covariance­and­contravarianchttp://stackoverflow.com/questions/8481301/covariance­invariance­and­contravariance­explained­in­plain­englishhttp://stackoverflow.com/questions/2501023/demonstrate­covariance­and­contravariance­in­java#2501513

Page 50: Covariance and contravariance. Say what?! (Agile Talks #22)

Image resourceshttp://www.luchsinger­mathematics.ch/Var_Reduction.jpghttps://i.kinja­img.com/gawker­media/image/upload/s­­7lQpqRh­­­/c_scale,f_auto,fl_progressive,q_80,w_800/18ncmqm1vbv9vjpg.jpghttp://classrealm.com/blog/wp­content/uploads/2012/03/WAildmath­300x270.jpghttp://www.i­programmer.info/images/stories/Core/Theory/covariance/arrowscontraandco.jpghttp://www.jonshawtrumpet.com/uploads/2/8/2/1/28211373/1670275_orig.jpghttps://www.memecreator.org/static/images/memes/3629822.jpghttp://i2.kym­cdn.com/photos/images/newsfeed/000/085/283/philosoraptor.jpghttps://cdn.meme.am/cache/instances/folder757/500x/65401757/katt­williams­shocked­let­me­rephrase­that­any­questions­i­can­answer.jpghttps://pixabay.com/en/puzzle­game­solution­connection­226743/https://pixabay.com/en/blur­cellphone­close­up­design­1867748/http://www.moneyloveandlegacy.com/images/HandsGivingCoins.jpghttp://hivhomekit.com/wp­content/uploads/2012/02/generic­stamp.pnghttps://i.ytimg.com/vi/uCpirskzaO8/hqdefault.jpg