Skip to content

Commit

Permalink
Merge pull request #404 from softwaremill/id
Browse files Browse the repository at this point in the history
Add Identity type alias & monad implementation
  • Loading branch information
adamw committed May 27, 2024
2 parents e230094 + abbe8f3 commit 7d94944
Show file tree
Hide file tree
Showing 2 changed files with 25 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
}
9 changes: 9 additions & 0 deletions core/src/main/scala/sttp/shared/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package sttp

package object shared {

/** The `Identity` type constructor can be used where an "effect" or wrapper (usually called `F[_]`) is expected. It
* represents direct-style / synchronous programming, and allows passing in code written in this style.
*/
type Identity[X] = X
}

0 comments on commit 7d94944

Please sign in to comment.