Skip to content

Commit 7d94944

Browse files
authored
Merge pull request #404 from softwaremill/id
Add Identity type alias & monad implementation
2 parents e230094 + abbe8f3 commit 7d94944

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

core/src/main/scala/sttp/monad/MonadError.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package sttp.monad
22

3+
import sttp.shared.Identity
4+
35
import scala.concurrent.{ExecutionContext, Future, Promise}
46
import scala.util.{Failure, Success, Try}
57

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

170172
override def blocking[T](t: => T): Future[T] = Future(scala.concurrent.blocking(t))
171173
}
174+
175+
object IdentityMonad extends MonadError[Identity] {
176+
override def unit[T](t: T): Identity[T] = t
177+
override def map[T, T2](fa: Identity[T])(f: T => T2): Identity[T2] = f(fa)
178+
override def flatMap[T, T2](fa: Identity[T])(f: T => Identity[T2]): Identity[T2] = f(fa)
179+
override def error[T](t: Throwable): Identity[T] = throw t
180+
override protected def handleWrappedError[T](rt: Identity[T])(
181+
h: PartialFunction[Throwable, Identity[T]]
182+
): Identity[T] = rt
183+
override def eval[T](t: => T): Identity[T] = t
184+
override def ensure[T](f: Identity[T], e: => Identity[Unit]): Identity[T] =
185+
try f
186+
finally e
187+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package sttp
2+
3+
package object shared {
4+
5+
/** The `Identity` type constructor can be used where an "effect" or wrapper (usually called `F[_]`) is expected. It
6+
* represents direct-style / synchronous programming, and allows passing in code written in this style.
7+
*/
8+
type Identity[X] = X
9+
}

0 commit comments

Comments
 (0)