typelevel / typelevel/cats

Proposal: lifting between monad transformers, simple effect types and raw types

Open
#2,661 3 comments 4 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

In practice, I often encounter situations when I need to lift F[_] to EitherT or OptionT. For EitherT, the only current option is EitherT.right[E](foo: F[A]): EitherT[F, E, A]. Compare that to A-F[A] interop or OptionT-EitherT interop:

(a: A).pure[F]: F[A]
(o: OptionT[F, A]).toRight(error: E): EitherT[F, E, A]
(e: EitherT[F, E, A]).toOption: OptionT[F, A]

Similarly, I think it is a good idea to have rich wrappers such as to allow:

(fa: F[A]).toRight[E]: EitherT[F, E, A]
(fa: F[A]).toLeft[B]: EitherT[F, A, B]
(fe: F[Either[E, A]]).lift: EitherT[F, E, A]
(fo: F[Option[A]]).lift: OptionT[F, A]

The EitherT.right(foo: F[A]) is a bit cumbersome because:

  • There's a redundancy of information: in general, right uniquely implies EitherT when used on F[A]
  • You could solve the above redundancy by importing EitherT.{right, ...} – however, there is a bunch of conversion methods defined on EitherT, and the need to manage the imports might introduce some mental overhead. Ideally, I'd like to be able to just import Cats once (e.g. currently I am using import cats._, cats.implicits._, cats.data._, cats.effect._) and forget about it.
  • There are extra parentheses: right[E](fa) vs fa.toRight. Parentheses clutter code.
  • The syntax is not uniform with that of A: if I can do a.pure[EitherT[...]], I'd also like to be able to do fa.toRight[E].

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 by reading the existing EitherT.right entry point and the OptionT/EitherT conversion methods described in the proposal. Compare their syntax and semantics with the requested F[A].toRight, F[A].toLeft, and F[Either] or F[Option] lifting APIs; done requires an agreed scope and corresponding implementation and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.