-
Notifications
You must be signed in to change notification settings - Fork 451
/
Copy pathresult.kt
54 lines (44 loc) · 1.85 KB
/
result.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package arrow.core.continuations
import arrow.core.identity
import kotlin.jvm.JvmInline
public suspend fun <A> Effect<Throwable, A>.toResult(): Result<A> =
fold({ Result.failure(it) }, { Result.success(it) })
public fun <A> EagerEffect<Throwable, A>.toResult(): Result<A> =
fold({ Result.failure(it) }, { Result.success(it) })
@JvmInline
public value class ResultEffectScope(private val cont: EffectScope<Throwable>) : EffectScope<Throwable> {
override suspend fun <B> shift(r: Throwable): B =
cont.shift(r)
public suspend fun <B> Result<B>.bind(): B =
fold(::identity) { shift(it) }
}
@JvmInline
public value class ResultEagerEffectScope(private val cont: EagerEffectScope<Throwable>) : EagerEffectScope<Throwable> {
@Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL")
override suspend fun <B> shift(r: Throwable): B =
cont.shift(r)
public suspend fun <B> Result<B>.bind(): B =
fold(::identity) { shift(it) }
}
@Deprecated(resultDSLDeprecation, ReplaceWith("result", "arrow.core.raise.result"))
@Suppress("ClassName")
public object result {
@Deprecated(
resultDSLDeprecation,
ReplaceWith("result(f)", "arrow.core.raise.result")
)
public inline fun <A> eager(crossinline f: suspend ResultEagerEffectScope.() -> A): Result<A> =
eagerEffect<Throwable, A> {
@Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL")
f(ResultEagerEffectScope(this))
}.toResult()
@Deprecated(
resultDSLDeprecation,
ReplaceWith("result(f)", "arrow.core.raise.result")
)
public suspend inline operator fun <A> invoke(crossinline f: suspend ResultEffectScope.() -> A): Result<A> =
effect<Throwable, A> { f(ResultEffectScope(this)) }.toResult()
}
private const val resultDSLDeprecation =
"The result DSL has been moved to arrow.core.raise.result.\n" +
"Replace import arrow.core.computations.* with arrow.core.raise.*"