Contravariance

From APIDesign

Jump to: navigation, search

Everything one would ever like to know about Covariance and Contravariance is available at wikipedia. Here is just a small example how it does not work in Java (the argumentation is the same as in case of Covariance). First version would like to be fixed:

does not exists: variance.contravariance.v1

However when one tries to change the parameter type:

does not exists: variance.contravariance.v2

Existing code would compile against the new library. However because the rules for looking up the right method are different between JavaC and JVM, we'll have a problem. Code like this will compile fine, but...

does not exists: variance.contravariance.test

...if compiled against version 1.0 and executed against version 2.0, it will throw linkage errors:

Mixing should not work: Compiling with version 1.0 and running against version 2.0 fails.
Exception in thread "main" java.lang.NoSuchMethodError: api.Contravariance.isPositive(Ljava/lang/Integer;)Z
	at test.ContravarianceTest.main(Unknown Source)
Java Result: 1

More info at Covariance. For possible trick around this behavior see Erasure.

Personal tools
buy