-
Notifications
You must be signed in to change notification settings - Fork 449
/
option.kt
80 lines (66 loc) · 2.44 KB
/
option.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package arrow.core.continuations
import arrow.core.None
import arrow.core.Option
import arrow.core.Some
import arrow.core.identity
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
import kotlin.jvm.JvmInline
public suspend fun <A> Effect<None, A>.toOption(): Option<A> =
fold(::identity) { Some(it) }
public fun <A> EagerEffect<None, A>.toOption(): Option<A> =
fold(::identity) { Some(it) }
@JvmInline
public value class OptionEffectScope(private val cont: EffectScope<None>) : EffectScope<None> {
override suspend fun <B> shift(r: None): B =
cont.shift(r)
public suspend fun <B> Option<B>.bind(): B =
bind { None }
public suspend fun ensure(value: Boolean): Unit =
ensure(value) { None }
}
@OptIn(ExperimentalContracts::class)
public suspend fun <B> OptionEffectScope.ensureNotNull(value: B?): B {
contract { returns() implies (value != null) }
return ensureNotNull(value) { None }
}
@OptIn(ExperimentalContracts::class)
public suspend fun <B> OptionEagerEffectScope.ensureNotNull(value: B?): B {
contract { returns() implies (value != null) }
return ensureNotNull(value) { None }
}
@JvmInline
public value class OptionEagerEffectScope(private val cont: EagerEffectScope<None>) : EagerEffectScope<None> {
@Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL")
override suspend fun <B> shift(r: None): B =
cont.shift(r)
public suspend fun <B> Option<B>.bind(): B =
bind { None }
public suspend fun ensure(value: Boolean): Unit =
ensure(value) { None }
}
@Deprecated(
optionDSLDeprecation,
ReplaceWith("option", "arrow.core.raise.option")
)
@Suppress("ClassName")
public object option {
@Deprecated(
optionDSLDeprecation,
ReplaceWith("option(f)", "arrow.core.raise.option")
)
public inline fun <A> eager(crossinline f: suspend OptionEagerEffectScope.() -> A): Option<A> =
eagerEffect<None, A> {
@Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL")
f(OptionEagerEffectScope(this))
}.toOption()
@Deprecated(
optionDSLDeprecation,
ReplaceWith("option(f)", "arrow.core.raise.option")
)
public suspend inline operator fun <A> invoke(crossinline f: suspend OptionEffectScope.() -> A): Option<A> =
effect<None, A> { f(OptionEffectScope(this)) }.toOption()
}
private const val optionDSLDeprecation =
"The option DSL has been moved to arrow.core.raise.option.\n" +
"Replace import arrow.core.computations.option with arrow.core.raise.option"