Idea: covariant monad transformers with invariant
Nobody has claimed this yet.
- Dominant language
- Scala
- Stars
- 5.5k
- Forks
- 1.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 5
Description
Not sure if this has been explored before.
The reason why monad transformers couldn't be covariant in their result / extra types was mostly that if the transformer were to be covariant, we would need to make the F[_] covariant as well (F[+_]).
Here's an alternative idea for making it work:
import cats.syntax.all._
import cats._
class EitherT[F[_], +E, +A](value: F[Either[E, A]]) {
def run[E2 >: E, A2 >: A](implicit F: Functor[F]): F[Either[E2, A2]] =
value.widen
def flatMap[E2 >: E, B](
f: A => EitherT[F, E2, B]
)(implicit F: Monad[F]): EitherT[F, E2, B] =
new EitherT[F, E2, B]({
value.flatMap {
case Left(e) => F.pure(Left(e))
case Right(v) => f(v).run
}
})
//...and others
}
This would require some work to ensure backwards binary compatibility (if possible at all), and could potentially break source compat as well, but maybe the idea hasn't been explored before - what do you think?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the proposed EitherT API and its Functor and Monad constraints, then compare the idea with the existing transformer design. Determine whether covariant result and error types can preserve binary and source compatibility; done means documenting a feasibility decision and any required scope.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- scala
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100