Skip to content

Commit

Permalink
Add Identity type alias & monad implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed May 27, 2024
1 parent e230094 commit 2f10be6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core/src/main/scala/sttp/monad/MonadError.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sttp.monad

import sttp.shared.Identity

import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.util.{Failure, Success, Try}

Expand Down Expand Up @@ -169,3 +171,17 @@ class FutureMonad(implicit ec: ExecutionContext) extends MonadAsyncError[Future]

override def blocking[T](t: => T): Future[T] = Future(scala.concurrent.blocking(t))
}

object IdentityMonad extends MonadError[Identity] {
override def unit[T](t: T): Identity[T] = t
override def map[T, T2](fa: Identity[T])(f: T => T2): Identity[T2] = f(fa)
override def flatMap[T, T2](fa: Identity[T])(f: T => Identity[T2]): Identity[T2] = f(fa)
override def error[T](t: Throwable): Identity[T] = throw t
override protected def handleWrappedError[T](rt: Identity[T])(
h: PartialFunction[Throwable, Identity[T]]
): Identity[T] = rt
override def eval[T](t: => T): Identity[T] = t
override def ensure[T](f: Identity[T], e: => Identity[Unit]): Identity[T] =
try f
finally e
}
5 changes: 5 additions & 0 deletions core/src/main/scala/sttp/shared/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sttp

package object shared {
type Identity[X] = X
}

0 comments on commit 2f10be6

Please sign in to comment.