typelevel / typelevel/cats

Idea: covariant monad transformers with invariant

Open
#3,819 1 comment 2 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.