diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt index c0ce588..ade93e5 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt @@ -251,14 +251,9 @@ public inline fun Result.toErrorIf(predicate: (V) -> Boolean, trans callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } - return when (this) { - is Ok -> if (predicate(value)) { - Err(transform(value)) - } else { - this - } - - is Err -> this + return when { + this is Ok && predicate(value) -> Err(transform(value)) + else -> this } } diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Or.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Or.kt index d74900a..fc4063a 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Or.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Or.kt @@ -62,13 +62,9 @@ public inline fun Result.throwIf(predicate: (E) -> Bool callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) } - return when (this) { - is Ok -> this - is Err -> if (predicate(error)) { - throw error - } else { - this - } + return when { + this is Err && predicate(error) -> throw error + else -> this } } diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Recover.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Recover.kt index 2618000..a949617 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Recover.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Recover.kt @@ -43,13 +43,9 @@ public inline fun Result.recoverIf(predicate: (E) -> Boolean, trans callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } - return when (this) { - is Ok -> this - is Err -> if (predicate(error)) { - Ok(transform(error)) - } else { - this - } + return when { + this is Err && predicate(error) -> Ok(transform(error)) + else -> this } } @@ -63,13 +59,9 @@ public inline fun Result.recoverUnless(predicate: (E) -> Boolean, t callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } - return when (this) { - is Ok -> this - is Err -> if (!predicate(error)) { - Ok(transform(error)) - } else { - this - } + return when { + this is Err && !predicate(error) -> Ok(transform(error)) + else -> this } } @@ -101,13 +93,9 @@ public inline fun Result.andThenRecoverIf( callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } - return when (this) { - is Ok -> this - is Err -> if (predicate(error)) { - transform(error) - } else { - this - } + return when { + this is Err && predicate(error) -> transform(error) + else -> this } } @@ -124,12 +112,8 @@ public inline fun Result.andThenRecoverUnless( callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } - return when (this) { - is Ok -> this - is Err -> if (!predicate(error)) { - transform(error) - } else { - this - } + return when { + this is Err && !predicate(error) -> transform(error) + else -> this } }