Forgetted Invariant Functors !
People tend to forget this quite often. An invariant functor or an exponential functor is:
That's the famous
Functor ! Covariant functor implements xmap by discarding
g: B => A
As expected, it discards
f: A => B and makes use of
contramap to implement
Example for Contravariant Functor
Example for covariant Functor
As expected, it is
DecodeJson, where the type parameter in the type class comes at covariant position (method result)
If type parameters are at covariant position, that means the method return contains the type.
If type parameters are at contravariant position, that means the method parameters contain the type.
When is invariant functor?
We may have types at covariant (output) or contravariant (input) position. However, we may sometime deal with both covariance and contravariance in the same type class.
Let's bring in EncodeJson and DecodeJson into one type class.
EncodeJson and DecodeJson
Functor but invariant
So an individual
contramap to upcast (or downcast) an A to B in the context of
F[_] is not possible if
F has types both in covariant and contravariant positions. It means,
F has to have an invariant functor for it!