I believe one can define covariance (at least, for objects) as ‘the ability to use a value of a narrower (sub) type in place of a value of some wider (super) type’, and that contravariance is the exact opposite of this.
Apparently, Scala functions are instances of Function[-A1,…,+B] for contravariant parameter types A1, etc. and covariant return type, B. While this is handy for subtyping on Functions, shouldn’t the above definition mean I can pass any supertypes as parameters?
Please advise where I’m mistaken.
Covariance and contravariance are qualities of the class not qualities of the parameters. (They are qualities that depend on the parameters, but they make statements about the class.)
So,
Function1[-A,+B]means that a function that takes superclasses ofAcan be viewed as a subclass of the original function.Let’s see this in practice:
Now suppose you require a function that knows how to print a
B:You could pass in
printB. But you could also pass inprintA, since it also knows how to printBs (and more!), just as ifA => Unitwas a subclass ofB => Unit. This is exactly what contravariance means. It doesn’t mean you can passOption[Double]intoprintBand get anything but a compile-time error!(Covariance is the other case:
M[B] <: M[A]ifB <: A.)