Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}