-
Notifications
You must be signed in to change notification settings - Fork 455
/
Copy pathBuilders.kt
335 lines (289 loc) · 10.1 KB
/
Builders.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
@file:JvmMultifileClass
@file:JvmName("RaiseKt")
@file:OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class)
package arrow.core.raise
import arrow.atomic.Atomic
import arrow.atomic.updateAndGet
import arrow.core.Either
import arrow.core.Ior
import arrow.core.NonEmptyList
import arrow.core.NonEmptySet
import arrow.core.None
import arrow.core.Option
import arrow.core.Some
import arrow.core.getOrElse
import arrow.core.identity
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
import kotlin.experimental.ExperimentalTypeInference
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
/**
* Runs a computation [block] using [Raise], and return its outcome as [Either].
* - [Either.Right] represents success,
* - [Either.Left] represents logical failure.
*
* This function re-throws any exceptions thrown within the [Raise] block.
*
* Read more about running a [Raise] computation in the
* [Arrow docs](https://arrow-kt.io/learn/typed-errors/working-with-typed-errors/#running-and-inspecting-results).
*/
public inline fun <Error, A> either(@BuilderInference block: Raise<Error>.() -> A): Either<Error, A> =
fold({ block.invoke(this) }, { Either.Left(it) }, { Either.Right(it) })
/**
* Runs a computation [block] using [Raise], and return its outcome as nullable type,
* where `null` represents logical failure.
*
* This function re-throws any exceptions thrown within the [Raise] block.
*
* Read more about running a [Raise] computation in the
* [Arrow docs](https://arrow-kt.io/learn/typed-errors/working-with-typed-errors/#running-and-inspecting-results).
*/
public inline fun <A> nullable(block: NullableRaise.() -> A): A? =
merge { block(NullableRaise(this)) }
/**
* Runs a computation [block] using [Raise], and return its outcome as [Result].
*
*
* Read more about running a [Raise] computation in the
* [Arrow docs](https://arrow-kt.io/learn/typed-errors/working-with-typed-errors/#running-and-inspecting-results).
*/
public inline fun <A> result(block: ResultRaise.() -> A): Result<A> =
fold({ block(ResultRaise(this)) }, Result.Companion::failure, Result.Companion::failure, Result.Companion::success)
/**
* Runs a computation [block] using [Raise], and return its outcome as [Option].
* - [Some] represents success,
* - [None] represents logical failure.
*
* This function re-throws any exceptions thrown within the [Raise] block.
*
* Read more about running a [Raise] computation in the
* [Arrow docs](https://arrow-kt.io/learn/typed-errors/working-with-typed-errors/#running-and-inspecting-results).
*/
public inline fun <A> option(block: OptionRaise.() -> A): Option<A> =
fold({ block(OptionRaise(this)) }, ::identity, ::Some)
/**
* Runs a computation [block] using [Raise], and return its outcome as [Ior].
* - [Ior.Right] represents success,
* - [Ior.Left] represents logical failure which made it impossible to continue,
* - [Ior.Both] represents that some logical failures were raised,
* but it was possible to continue until producing a final value.
*
* This function re-throws any exceptions thrown within the [Raise] block.
*
* In both [Ior.Left] and [Ior.Both] cases, if more than one logical failure
* has been raised, they are combined using [combineError].
*
* Read more about running a [Raise] computation in the
* [Arrow docs](https://arrow-kt.io/learn/typed-errors/working-with-typed-errors/#running-and-inspecting-results).
*/
public inline fun <Error, A> ior(noinline combineError: (Error, Error) -> Error, @BuilderInference block: IorRaise<Error>.() -> A): Ior<Error, A> {
val state: Atomic<Option<Error>> = Atomic(None)
return fold<Error, A, Ior<Error, A>>(
{ block(IorRaise(combineError, state, this)) },
{ e -> throw e },
{ e -> Ior.Left(state.get().getOrElse { e }) },
{ a -> state.get().fold({ Ior.Right(a) }, { Ior.Both(it, a) }) }
)
}
/**
* Implementation of [Raise] used by `ignoreErrors`.
* You should never use this directly.
*/
public class IgnoreErrorsRaise<N>(
private val raise: Raise<N>,
private val error: () -> N
) : Raise<Any?> {
@RaiseDSL
override fun raise(r: Any?): Nothing =
raise.raise(error())
@RaiseDSL
public fun ensure(value: Boolean): Unit = ensure(value) { null }
@RaiseDSL
public fun <A> Option<A>.bind(): A = getOrElse { raise(null) }
@RaiseDSL
public fun <A> A?.bind(): A {
contract { returns() implies (this@bind != null) }
return this ?: raise(null)
}
@RaiseDSL
public fun <A> ensureNotNull(value: A?): A {
contract { returns() implies (value != null) }
return ensureNotNull(value) { null }
}
}
public typealias Null = Nothing?
/**
* Implementation of [Raise] used by [nullable].
* You should never use this directly.
*/
public class NullableRaise(private val raise: Raise<Null>) : Raise<Null> by raise {
@RaiseDSL
public fun ensure(value: Boolean): Unit = ensure(value) { null }
@RaiseDSL
public fun <A> Option<A>.bind(): A = getOrElse { raise(null) }
@RaiseDSL
public fun <A> A?.bind(): A {
contract { returns() implies (this@bind != null) }
return this ?: raise(null)
}
@JvmName("bindAllNullable")
public fun <K, V> Map<K, V?>.bindAll(): Map<K, V> =
mapValues { (_, v) -> v.bind() }
@RaiseDSL
@JvmName("bindAllNullable")
public fun <A> Iterable<A?>.bindAll(): List<A> =
map { it.bind() }
@RaiseDSL
public fun <A> ensureNotNull(value: A?): A {
contract { returns() implies (value != null) }
return ensureNotNull(value) { null }
}
@RaiseDSL
public inline fun <A> recover(
@BuilderInference block: NullableRaise.() -> A,
recover: () -> A,
): A = when (val nullable = nullable(block)) {
null -> recover()
else -> nullable
}
/**
* Introduces a scope where you can [bind] errors of any type,
* but no information is saved in the [raise] case.
*/
@RaiseDSL
public inline fun <A> ignoreErrors(
@BuilderInference block: IgnoreErrorsRaise<Null>.() -> A,
): A = block(IgnoreErrorsRaise(this) { null })
}
/**
* Implementation of [Raise] used by [result].
* You should never use this directly.
*/
public class ResultRaise(private val raise: Raise<Throwable>) : Raise<Throwable> by raise {
@RaiseDSL
public fun <A> Result<A>.bind(): A = fold(::identity) { raise(it) }
@JvmName("bindAllResult")
public fun <K, V> Map<K, Result<V>>.bindAll(): Map<K, V> =
mapValues { (_, v) -> v.bind() }
@RaiseDSL
@JvmName("bindAllResult")
public fun <A> Iterable<Result<A>>.bindAll(): List<A> =
map { it.bind() }
@RaiseDSL
@JvmName("bindAllResult")
public fun <A> NonEmptyList<Result<A>>.bindAll(): NonEmptyList<A> =
map { it.bind() }
@RaiseDSL
@JvmName("bindAllResult")
public fun <A> NonEmptySet<Result<A>>.bindAll(): NonEmptySet<A> =
map { it.bind() }.toNonEmptySet()
@RaiseDSL
public inline fun <A> recover(
@BuilderInference block: ResultRaise.() -> A,
recover: (Throwable) -> A,
): A = result(block).fold(
onSuccess = { it },
onFailure = { recover(it) }
)
}
/**
* Implementation of [Raise] used by [option].
* You should never use this directly.
*/
public class OptionRaise(private val raise: Raise<None>) : Raise<None> by raise {
@RaiseDSL
public fun <A> Option<A>.bind(): A = getOrElse { raise(None) }
@JvmName("bindAllOption")
public fun <K, V> Map<K, Option<V>>.bindAll(): Map<K, V> =
mapValues { (_, v) -> v.bind() }
@RaiseDSL
@JvmName("bindAllOption")
public fun <A> Iterable<Option<A>>.bindAll(): List<A> =
map { it.bind() }
@RaiseDSL
@JvmName("bindAllOption")
public fun <A> NonEmptyList<Option<A>>.bindAll(): NonEmptyList<A> =
map { it.bind() }
@RaiseDSL
@JvmName("bindAllOption")
public fun <A> NonEmptySet<Option<A>>.bindAll(): NonEmptySet<A> =
map { it.bind() }.toNonEmptySet()
@RaiseDSL
public fun ensure(value: Boolean): Unit = ensure(value) { None }
@RaiseDSL
public fun <A> ensureNotNull(value: A?): A {
contract { returns() implies (value != null) }
return ensureNotNull(value) { None }
}
@RaiseDSL
public inline fun <A> recover(
@BuilderInference block: OptionRaise.() -> A,
recover: () -> A,
): A = when (val option = option(block)) {
is None -> recover()
is Some<A> -> option.value
}
/**
* Introduces a scope where you can [bind] errors of any type,
* but no information is saved in the [raise] case.
*/
@RaiseDSL
public inline fun <A> ignoreErrors(
@BuilderInference block: IgnoreErrorsRaise<None>.() -> A,
): A = block(IgnoreErrorsRaise(this) { None })
}
/**
* Implementation of [Raise] used by [ior].
* You should never use this directly.
*/
public class IorRaise<Error> @PublishedApi internal constructor(
@PublishedApi internal val combineError: (Error, Error) -> Error,
private val state: Atomic<Option<Error>>,
private val raise: Raise<Error>,
) : Raise<Error> {
@RaiseDSL
override fun raise(r: Error): Nothing = raise.raise(combine(r))
@RaiseDSL
@JvmName("bindAllIor")
public fun <A> Iterable<Ior<Error, A>>.bindAll(): List<A> =
map { it.bind() }
@RaiseDSL
@JvmName("bindAllIor")
public fun <A> NonEmptyList<Ior<Error, A>>.bindAll(): NonEmptyList<A> =
map { it.bind() }
@RaiseDSL
@JvmName("bindAllIor")
public fun <A> NonEmptySet<Ior<Error, A>>.bindAll(): NonEmptySet<A> =
map { it.bind() }.toNonEmptySet()
@RaiseDSL
public fun <A> Ior<Error, A>.bind(): A =
when (this) {
is Ior.Left -> raise(value)
is Ior.Right -> value
is Ior.Both -> {
combine(leftValue)
rightValue
}
}
@JvmName("bindAllIor")
public fun <K, V> Map<K, Ior<Error, V>>.bindAll(): Map<K, V> =
mapValues { (_, v) -> v.bind() }
@PublishedApi
internal fun combine(other: Error): Error =
state.updateAndGet { prev ->
Some(prev.map { combineError(it, other) }.getOrElse { other })
}.getOrElse { other }
@RaiseDSL
public inline fun <A> recover(
@BuilderInference block: IorRaise<Error>.() -> A,
recover: (error: Error) -> A,
): A = when (val ior = ior(combineError, block)) {
is Ior.Both -> {
combine(ior.leftValue)
ior.rightValue
}
is Ior.Left -> recover(ior.value)
is Ior.Right -> ior.value
}
}