score:0

This happens because Scalaz does not define Monad as being covariant in its first type argument (or, more precisely, type constructor argument). In other words, a Monad[A] is considered a different type altogether from a Monad[B], even if A <: B. (More on covariance and contravariance)

There are good reasons why Monad is invariant. One is: if you make the compiler believe that a Monad[Id] is actually also valid as a Monad[ExtendedId], you're bound to run into problems at some point — one of them is that wherever pure is called, the compiler will infer an ExtendedId result type, whereas only an Id would be returned.

I think there isn't a technique to fix this cleanly — other than defining a Monad[ExtendedId], or something like

implicit def idMonad[A[_] <: Id[_]]: Monad[A] = ...

which indeed is able to return a proper monad for all subclasses of Id.


Related Query

More Query from same tag