Skip to content

Commit

Permalink
Adds Result.orThrow and Result.flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
Synesso committed Jun 19, 2024
1 parent 05c8486 commit 83508cf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added
* Adds `Result.catch` to enable the same behaviour as `Either.catch` (Alejandro Metke)
* Adds `Result<Result<T>>.flatten(): Result<T>` (Jem Mawson)

### Fixed
* `T.failure(): Result<T>` was invalid. Changed to `Throwable.failure(): Result<A>` (Jem Mawson)
Expand Down
2 changes: 2 additions & 0 deletions lib/api/lib.api
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ public final class app/cash/quiver/extensions/OptionKt {

public final class app/cash/quiver/extensions/ResultKt {
public static final fun failure (Ljava/lang/Throwable;)Ljava/lang/Object;
public static final fun flatten (Ljava/lang/Object;)Ljava/lang/Object;
public static final fun mapFailure (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun orThrow (Ljava/lang/Object;)Ljava/lang/Object;
public static final fun success (Ljava/lang/Object;)Ljava/lang/Object;
public static final fun toEither (Ljava/lang/Object;)Larrow/core/Either;
public static final fun toResult (Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
Expand Down
13 changes: 13 additions & 0 deletions lib/src/main/kotlin/app/cash/quiver/extensions/Result.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package app.cash.quiver.extensions

import arrow.core.Either
import arrow.core.flatMap
import arrow.core.getOrElse
import arrow.core.identity
import arrow.core.left
import arrow.core.right

Expand Down Expand Up @@ -45,3 +48,13 @@ inline fun <N : Throwable, T> Result<T>.mapFailure(
@JvmName("tryCatch")
inline fun <R> Result.Companion.catch(f: () -> R): Result<R> =
arrow.core.raise.catch({ success(f()) }) { failure(it) }

/**
* Retrieves the success of a Result, or throws the failure. Alias of `getOrThrow`, included for consistency with ErrorOr.
*/
fun <A> Result<A>.orThrow() = getOrThrow()

/**
* Flattens a `Result<Result<T>>` into a `Result<T>`
*/
fun <T> Result<Result<T>>.flatten(): Result<T> = flatMap(::identity)
12 changes: 12 additions & 0 deletions lib/src/test/kotlin/app/cash/quiver/extensions/ResultTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ class ResultTest : StringSpec({
}
}
}

"Result.orThrow" {
Result.success("世界").orThrow() shouldBe "世界"
shouldThrow<RuntimeException> { Result.failure<Unit>(RuntimeException("thrown")).orThrow() }
}

"Result<Result<T>>.flatten" {
val exception = RuntimeException("thrown")
Result.success(Result.success("hello")).flatten() shouldBeSuccess "hello"
Result.success(Result.failure<String>(exception)).flatten() shouldBeFailure exception
Result.failure<Result<String>>(exception).flatten() shouldBeFailure exception
}
})

0 comments on commit 83508cf

Please sign in to comment.