Skip to content

Add Hande/Raise convenience syntax#684

Merged
djspiewak merged 2 commits into
typelevel:mainfrom
satorg:add-raise-syntax
Mar 12, 2026
Merged

Add Hande/Raise convenience syntax#684
djspiewak merged 2 commits into
typelevel:mainfrom
satorg:add-raise-syntax

Conversation

@satorg

@satorg satorg commented Feb 4, 2026

Copy link
Copy Markdown
Contributor

Handle.allow approach is great but it lacks some convenience syntax that is avaliable in Cats. This PR proposes the following syntax:

  • for Option:

    • liftTo:

      Details
      import scala.util.*
      import cats.mtl.*, syntax.option.*
      
      case class MyErr(err: String) extends Exception(err)
      
      val res1 =
        Handle.allow:
          Some(123).liftTo[Try]("OOPS")
        .rescue: err =>
          Failure(MyErr(err))
      
      assertEquals(res1, Success(123))
      
      val res2 =
        Handle.allow:
          None.liftTo[Try]("OOPS")
        .rescue: err =>
          Failure(MyErr(err))
      
      assertEquals(res2, Failure(MyErr("OOPS")))    
    • raiseTo:

      Details
      import scala.util.*
      import cats.mtl.*, syntax.option.*
      
      case class MyErr(err: String) extends Exception(err)
      
      val res1 =
        Handle.allow:
          Some("OOPS").raiseTo[Try]
        .rescue: err =>
          Failure(MyErr(err))
      
      assertEquals(res1, Failure(MyErr("OOPS")))
      
      val res2 =
        Handle.allow:
          (None: Option[String]).raiseTo[Try]
        .rescue: err =>
          Failure(MyErr(err))
      
      assertEquals(res2, Success(()))
  • for Either:

    • liftTo:

      Details
      import scala.util.*
      import cats.mtl.*, syntax.either.*
      
      case class MyErr(err: String) extends Exception(err)
      
      val res1 =
        Handle.allow:
          Right[String, Int](123).liftTo[Try]
        .rescue: err =>
          Failure(MyErr(err))
      
      assertEquals(res1, Success(123))
      
      val res2 =
        Handle.allow:
          Left[String, Int]("OOPS").liftTo[Try]
        .rescue: err =>
          Failure(MyErr(err))
      
      assertEquals(res2, Failure("OOPS"))

@satorg satorg force-pushed the add-raise-syntax branch 2 times, most recently from a3c9bec to 756eaad Compare February 21, 2026 05:08
@satorg

satorg commented Feb 23, 2026

Copy link
Copy Markdown
Contributor Author

@LukaJCB thank you for the approval!
Just FYI: I still cannot merge it, perhaps I don't have write access here.

@satorg satorg force-pushed the add-raise-syntax branch from 7adf1d5 to 4285a10 Compare March 7, 2026 05:54
@djspiewak djspiewak merged commit df06166 into typelevel:main Mar 12, 2026
12 checks passed
@satorg satorg deleted the add-raise-syntax branch March 13, 2026 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants