2 ms·
Variance inevitable rears its ugly face when you introduce a subtyping relation to your type system. Variance is most visible in languages that mix parametric
by ufo 8y ago
Variance inevitable rears its ugly face when you introduce a subtyping relation to your type system.
Variance is most visible in languages that mix parametric polymorphism (generics) with subtyping (for example, OO subclassing) because when this happens you may need to write down explicit variance annotations to appease the type checker.
However, variance still matters even without parametric types. For example, it describes what types you can use when overriding a methof in a subclass. The subclass is allowed to be "more general" than the superclass so if you want you can change the input parameters of the method to a more general supertype and you can change the return type to a more specific subtype. The output types are covariant: in the subclass where you create the method you can use a subclass of the return type. The input types are contravariant: in the subclass where you create the method you use a superclass of the input types.