dave08
02/22/2023, 4:22 PMsimon.vergauwen
02/22/2023, 4:29 PMEither
is a value, where Effect<E, A>
is suspend Raise<E>.() -> A
.
Either
is the result of invoking (suspend) Raise<E>.() -> A
.simon.vergauwen
02/22/2023, 4:30 PMEffect<E, A>
I can invoke it many times, but if you pass me a Either<E, A>
value I can only inspect it but I cannot recompute it. If I need to recompute it then you need to pass me (suspend) () -> Either<E, A>
or (suspend) Raise<E>.() -> A
.simon.vergauwen
02/22/2023, 4:31 PMdave08
02/22/2023, 4:40 PMSam Painter
02/22/2023, 4:43 PMdave08
02/22/2023, 4:43 PMprivate suspend inline fun <reified A : Any> KtorCtx.respond(
statusCode: HttpStatusCode,
noinline action: suspend Raise<ServiceError>.() -> A
): Unit = effect(action).fold(
{ error ->
when (error) {
is ServiceError -> call.respond(BadRequest, error)
// is UserAlreadyExists -> call.respond(Conflict, "${error.user} already exists")
// is UserNotFound -> call.respond(NotFound, "User with id ${error.id} not found")
}
}) { a -> call.respond(statusCode, a) }
dave08
02/22/2023, 4:46 PMdave08
02/22/2023, 4:46 PMFor the same reasons you ever want a lazy value.So why would it be used in that previous case?
simon.vergauwen
02/22/2023, 4:47 PMsuspend fun one(): Either<String, Int> = TODO("")
fun two(): Effect<String, Int> =
effect { one().bind() }
suspend fun three(): Either<String, Int> =
either { two().bind() }
dave08
02/22/2023, 4:47 PMsimon.vergauwen
02/22/2023, 4:49 PMsuspend fun
is what makes one
and three
"lazy". Here they they have very different meaning however.
val one: Either<String, Int> = 1.right()
val two: Effect<String, Int> = effect { 1 }
You can inspect one
but you cannot do anything with two
unless you invoke it in some suspend fun
first.simon.vergauwen
02/22/2023, 4:50 PMRaise
is that it power everything in Arrow, it allows this entire mechanism of either { }
, ensure
, ensureNotNull
etc to work. All these APIs and DSLs are build on-top of it.simon.vergauwen
02/22/2023, 4:51 PMEffect
is also not even a type anymore, it's just a typealias
for a lambda.dave08
02/22/2023, 4:53 PMdave08
02/22/2023, 4:54 PMsimon.vergauwen
02/22/2023, 4:55 PMEither
is a computedEffect 😅 but it depends a bit on whether it was an Effect
or EagerEffect
since suspend
is allowed to pass through the runtimedave08
02/22/2023, 4:56 PMdave08
02/22/2023, 4:56 PMsimon.vergauwen
02/22/2023, 4:57 PMdave08
02/22/2023, 4:58 PMdave08
02/22/2023, 4:58 PMdave08
02/22/2023, 5:00 PMdave08
02/22/2023, 5:00 PMdave08
02/22/2023, 5:03 PMdave08
02/22/2023, 5:03 PMsimon.vergauwen
02/22/2023, 5:04 PMsimon.vergauwen
02/22/2023, 5:04 PM