3 ms·
Explaining it to a five-year-old adds overhead that isn't really useful when you're in fact explaining to someone who knows about programming but has simply nev
by victorNicollet 9y ago
Explaining it to a five-year-old adds overhead that isn't really useful when you're in fact explaining to someone who knows about programming but has simply never heard about covariance and contravariance.
A function `X -> string` can be turned into a function `X -> object`: call it, take its string result and cast it to object. This is called covariant (latin co- for "together") because casting `string` to `object` also lets you cast `X -> string` to `X -> object`.
A function `object -> X` can turned into a function `string -> X`: take the string argument, cast it to object, and pass it to the function. This is called contravariant (latin contra- for "against") because casting `string` to `object` lets you cast `object -> X` to `string -> X`.
- dotancohen 9y agoThanks, obviously your example is more concrete. GP's comment was good for introducing the idea and your comment is great for showing how the idea is useful.