Skip to content

Commit a7c66c0

Browse files
committed
Deprecate on{Success,Failure} terminology in favour of on{Ok,Err}
The new indexed variants prove that the naming becomes too verbose, so going with Ok/Err in the function names proves more readable: - onSuccess → onOk - onFailure → onErr - onEachSuccess → onEachOk - onEachSuccessIndexed → onEachOkIndexed - onEachFailure → onEachErr - onEachFailureIndexed → onEachErrIndexed
1 parent 55fe646 commit a7c66c0

8 files changed

Lines changed: 60 additions & 40 deletions

File tree

kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/RunSuspendCatching.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import kotlin.contracts.InvocationKind
88
import kotlin.contracts.contract
99

1010
/**
11-
* Calls the specified function [block] and returns its encapsulated result if invocation was
12-
* successful, catching any [Throwable] exception that was thrown from the [block] function
13-
* execution and encapsulating it as a failure. If the encapsulated failure is a
14-
* [CancellationException], the exception is thrown to indicate _normal_ cancellation of a
15-
* coroutine.
11+
* Calls the specified function [block] and returns its encapsulated result if invocation was ok,
12+
* catching any [Throwable] exception that was thrown from the [block] function execution and
13+
* encapsulating it as an error. If the encapsulated error is a [CancellationException], the
14+
* exception is thrown to indicate _normal_ cancellation of a coroutine.
1615
*/
1716
public inline fun <V> runSuspendCatching(block: () -> V): Result<V, Throwable> {
1817
contract {
@@ -26,10 +25,10 @@ public inline fun <V> runSuspendCatching(block: () -> V): Result<V, Throwable> {
2625

2726
/**
2827
* Calls the specified function [block] with [this] value as its receiver and returns its
29-
* encapsulated result if invocation was successful, catching any [Throwable] exception that was
30-
* thrown from the [block] function execution and encapsulating it as a failure. If the
31-
* encapsulated failure is a [CancellationException], the exception is thrown to indicate _normal_
32-
* cancellation of a coroutine.
28+
* encapsulated result if invocation was ok, catching any [Throwable] exception that was thrown
29+
* from the [block] function execution and encapsulating it as an error. If the encapsulated error
30+
* is a [CancellationException], the exception is thrown to indicate _normal_ cancellation of a
31+
* coroutine.
3332
*/
3433
public inline infix fun <T, V> T.runSuspendCatching(block: T.() -> V): Result<V, Throwable> {
3534
contract {

kotlin-result-coroutines/src/commonTest/kotlin/com/github/michaelbull/result/coroutines/RunSuspendCatchingTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.github.michaelbull.result.coroutines
22

33
import com.github.michaelbull.result.Err
44
import com.github.michaelbull.result.Ok
5-
import com.github.michaelbull.result.onSuccess
5+
import com.github.michaelbull.result.onOk
66
import kotlinx.coroutines.CoroutineName
77
import kotlinx.coroutines.cancel
88
import kotlinx.coroutines.delay
@@ -27,7 +27,7 @@ class RunSuspendCatchingTest {
2727
}
2828

2929
// The coroutine should be cancelled before reaching here
30-
result.onSuccess { value = it }
30+
result.onOk { value = it }
3131
}
3232

3333
testScheduler.advanceTimeBy(2_000)

kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Deprecations.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.github.michaelbull.result
22

3+
import kotlin.contracts.InvocationKind
4+
import kotlin.contracts.contract
5+
36
@Deprecated("Use filterOk instead.", ReplaceWith("filterOk()"))
47
public fun <V, E> Iterable<Result<V, E>>.filterValues(): List<V> {
58
return filterOk()
@@ -19,3 +22,21 @@ public fun <V, E, C : MutableCollection<in V>> Iterable<Result<V, E>>.filterValu
1922
public fun <V, E, C : MutableCollection<in E>> Iterable<Result<V, E>>.filterErrorsTo(destination: C): C {
2023
return filterErrTo(destination)
2124
}
25+
26+
@Deprecated("Use onOk instead.", ReplaceWith("onOk(action)"))
27+
public inline infix fun <V, E> Result<V, E>.onSuccess(action: (V) -> Unit): Result<V, E> {
28+
contract {
29+
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
30+
}
31+
32+
return onOk(action)
33+
}
34+
35+
@Deprecated("Use onErr instead.", ReplaceWith("onErr(action)"))
36+
public inline infix fun <V, E> Result<V, E>.onFailure(action: (E) -> Unit): Result<V, E> {
37+
contract {
38+
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
39+
}
40+
41+
return onErr(action)
42+
}

kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Factory.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import kotlin.contracts.InvocationKind
44
import kotlin.contracts.contract
55

66
/**
7-
* Calls the specified function [block] and returns its encapsulated result if invocation was
8-
* successful, catching any [Throwable] exception that was thrown from the [block] function
9-
* execution and encapsulating it as a failure.
7+
* Calls the specified function [block] and returns its encapsulated result if invocation was ok,
8+
* catching any [Throwable] exception that was thrown from the [block] function execution and
9+
* encapsulating it as an error.
1010
*/
1111
public inline fun <V> runCatching(block: () -> V): Result<V, Throwable> {
1212
contract {
@@ -22,8 +22,8 @@ public inline fun <V> runCatching(block: () -> V): Result<V, Throwable> {
2222

2323
/**
2424
* Calls the specified function [block] with [this] value as its receiver and returns its
25-
* encapsulated result if invocation was successful, catching any [Throwable] exception that was
26-
* thrown from the [block] function execution and encapsulating it as a failure.
25+
* encapsulated result if invocation was ok, catching any [Throwable] exception that was thrown
26+
* from the [block] function execution and encapsulating it as an error.
2727
*/
2828
public inline infix fun <T, V> T.runCatching(block: T.() -> V): Result<V, Throwable> {
2929
contract {

kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,17 +307,17 @@ public inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapRe
307307
* Performs the given [action] on each [ok][Result.isOk] value and returns the collection itself
308308
* afterwards.
309309
*/
310-
public inline fun <V, E> Iterable<Result<V, E>>.onEachSuccess(action: (V) -> Unit): Iterable<Result<V, E>> {
310+
public inline fun <V, E> Iterable<Result<V, E>>.onEachOk(action: (V) -> Unit): Iterable<Result<V, E>> {
311311
return onEach { result ->
312-
result.onSuccess(action)
312+
result.onOk(action)
313313
}
314314
}
315315

316316
/**
317317
* Performs the given [action] on each [ok][Result.isOk] value and its index and returns the
318318
* collection itself afterwards.
319319
*/
320-
public inline fun <V, E> Iterable<Result<V, E>>.onEachSuccessIndexed(action: (index: Int, V) -> Unit): Iterable<Result<V, E>> {
320+
public inline fun <V, E> Iterable<Result<V, E>>.onEachOkIndexed(action: (index: Int, V) -> Unit): Iterable<Result<V, E>> {
321321
return onEachIndexed { index, result ->
322322
if (result.isOk) {
323323
action(index, result.value)
@@ -329,17 +329,17 @@ public inline fun <V, E> Iterable<Result<V, E>>.onEachSuccessIndexed(action: (in
329329
* Performs the given [action] on each [error][Result.isErr] value and returns the collection itself
330330
* afterwards.
331331
*/
332-
public inline fun <V, E> Iterable<Result<V, E>>.onEachFailure(action: (E) -> Unit): Iterable<Result<V, E>> {
332+
public inline fun <V, E> Iterable<Result<V, E>>.onEachErr(action: (E) -> Unit): Iterable<Result<V, E>> {
333333
return onEach { result ->
334-
result.onFailure(action)
334+
result.onErr(action)
335335
}
336336
}
337337

338338
/**
339339
* Performs the given [action] on each [error][Result.isErr] value and its index and returns the
340340
* collection itself afterwards.
341341
*/
342-
public inline fun <V, E> Iterable<Result<V, E>>.onEachFailureIndexed(action: (index: Int, E) -> Unit): Iterable<Result<V, E>> {
342+
public inline fun <V, E> Iterable<Result<V, E>>.onEachErrIndexed(action: (index: Int, E) -> Unit): Iterable<Result<V, E>> {
343343
return onEachIndexed { index, result ->
344344
if (result.isErr) {
345345
action(index, result.error)

kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/On.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlin.contracts.contract
88
*
99
* - Rust: [Result.inspect](https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect)
1010
*/
11-
public inline infix fun <V, E> Result<V, E>.onSuccess(action: (V) -> Unit): Result<V, E> {
11+
public inline infix fun <V, E> Result<V, E>.onOk(action: (V) -> Unit): Result<V, E> {
1212
contract {
1313
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
1414
}
@@ -25,7 +25,7 @@ public inline infix fun <V, E> Result<V, E>.onSuccess(action: (V) -> Unit): Resu
2525
*
2626
* - Rust [Result.inspect_err](https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect_err)
2727
*/
28-
public inline infix fun <V, E> Result<V, E>.onFailure(action: (E) -> Unit): Result<V, E> {
28+
public inline infix fun <V, E> Result<V, E>.onErr(action: (E) -> Unit): Result<V, E> {
2929
contract {
3030
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
3131
}

kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/IterableTest.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class IterableTest {
338338
}
339339
}
340340

341-
class OnEachSuccess {
341+
class OnEachOk {
342342

343343
@Test
344344
fun invokesActionForOkResults() {
@@ -348,7 +348,7 @@ class IterableTest {
348348
Ok(1),
349349
Err("error"),
350350
Ok(2),
351-
).onEachSuccess(collected::add)
351+
).onEachOk(collected::add)
352352

353353
assertEquals(
354354
expected = listOf(1, 2),
@@ -364,7 +364,7 @@ class IterableTest {
364364
Ok(2),
365365
)
366366

367-
val result = items.onEachSuccess { }
367+
val result = items.onEachOk { }
368368

369369
assertEquals(
370370
expected = items,
@@ -373,7 +373,7 @@ class IterableTest {
373373
}
374374
}
375375

376-
class OnEachSuccessIndexed {
376+
class OnEachOkIndexed {
377377

378378
@Test
379379
fun invokesActionWithIndexForOkResults() {
@@ -385,7 +385,7 @@ class IterableTest {
385385
Ok(20),
386386
Err("error"),
387387
Ok(30),
388-
).onEachSuccessIndexed { index, value -> collected.add(index to value) }
388+
).onEachOkIndexed { index, value -> collected.add(index to value) }
389389

390390
assertEquals(
391391
expected = listOf(0 to 10, 2 to 20, 4 to 30),
@@ -394,7 +394,7 @@ class IterableTest {
394394
}
395395
}
396396

397-
class OnEachFailure {
397+
class OnEachErr {
398398

399399
@Test
400400
fun invokesActionForErrResults() {
@@ -405,7 +405,7 @@ class IterableTest {
405405
Err("error1"),
406406
Ok(2),
407407
Err("error2"),
408-
).onEachFailure(collected::add)
408+
).onEachErr(collected::add)
409409

410410
assertEquals(
411411
expected = listOf("error1", "error2"),
@@ -421,7 +421,7 @@ class IterableTest {
421421
Ok(2),
422422
)
423423

424-
val result = items.onEachFailure { }
424+
val result = items.onEachErr { }
425425

426426
assertEquals(
427427
expected = items,
@@ -430,7 +430,7 @@ class IterableTest {
430430
}
431431
}
432432

433-
class OnEachFailureIndexed {
433+
class OnEachErrIndexed {
434434

435435
@Test
436436
fun invokesActionWithIndexForErrResults() {
@@ -442,7 +442,7 @@ class IterableTest {
442442
Ok(2),
443443
Err("error2"),
444444
Ok(3),
445-
).onEachFailureIndexed { index, error -> collected.add(index to error) }
445+
).onEachErrIndexed { index, error -> collected.add(index to error) }
446446

447447
assertEquals(
448448
expected = listOf(1 to "error1", 3 to "error2"),

kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OnTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ class OnTest {
77
object CounterError
88
class Counter(var count: Int)
99

10-
class OnSuccess {
10+
class OnOk {
1111

1212
@Test
1313
fun invokesActionIfOk() {
1414
val counter = Counter(50)
1515

16-
Ok(counter).onSuccess { it.count += 50 }
16+
Ok(counter).onOk { it.count += 50 }
1717

1818
assertEquals(
1919
expected = 100,
@@ -25,7 +25,7 @@ class OnTest {
2525
fun invokesNothingIfErr() {
2626
val counter = Counter(200)
2727

28-
Err(CounterError).onSuccess { counter.count -= 50 }
28+
Err(CounterError).onOk { counter.count -= 50 }
2929

3030
assertEquals(
3131
expected = 200,
@@ -34,13 +34,13 @@ class OnTest {
3434
}
3535
}
3636

37-
class OnFailure {
37+
class OnErr {
3838

3939
@Test
4040
fun invokesActionIfErr() {
4141
val counter = Counter(555)
4242

43-
Err(CounterError).onFailure { counter.count += 100 }
43+
Err(CounterError).onErr { counter.count += 100 }
4444

4545
assertEquals(
4646
expected = 655,
@@ -52,7 +52,7 @@ class OnTest {
5252
fun invokesNothingIfOk() {
5353
val counter = Counter(1020)
5454

55-
Ok("hello").onFailure { counter.count = 1030 }
55+
Ok("hello").onErr { counter.count = 1030 }
5656

5757
assertEquals(
5858
expected = 1020,

0 commit comments

Comments
 (0)