Use Explicit API mode
See: https://kotlinlang.org/docs/reference/whatsnew14.html#explicit-api-mode-for-library-authors
This commit is contained in:
parent
620f434220
commit
a9a0c384f4
@ -64,6 +64,8 @@ subprojects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
configure<KotlinMultiplatformExtension> {
|
configure<KotlinMultiplatformExtension> {
|
||||||
|
explicitApi()
|
||||||
|
|
||||||
jvm {
|
jvm {
|
||||||
mavenPublication {
|
mavenPublication {
|
||||||
artifact(javadocJar.get())
|
artifact(javadocJar.get())
|
||||||
|
@ -12,7 +12,7 @@ import kotlin.contracts.contract
|
|||||||
/**
|
/**
|
||||||
* Suspending variant of [binding][com.github.michaelbull.result.binding].
|
* Suspending variant of [binding][com.github.michaelbull.result.binding].
|
||||||
*/
|
*/
|
||||||
suspend inline fun <V, E> binding(crossinline block: suspend SuspendableResultBinding<E>.() -> V): Result<V, E> {
|
public suspend inline fun <V, E> binding(crossinline block: suspend SuspendableResultBinding<E>.() -> V): Result<V, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||||
}
|
}
|
||||||
@ -28,8 +28,8 @@ suspend inline fun <V, E> binding(crossinline block: suspend SuspendableResultBi
|
|||||||
|
|
||||||
internal object BindCancellationException : CancellationException(null)
|
internal object BindCancellationException : CancellationException(null)
|
||||||
|
|
||||||
interface SuspendableResultBinding<E> {
|
public interface SuspendableResultBinding<E> {
|
||||||
suspend fun <V> Result<V, E>.bind(): V
|
public suspend fun <V> Result<V, E>.bind(): V
|
||||||
}
|
}
|
||||||
|
|
||||||
@PublishedApi
|
@PublishedApi
|
||||||
|
@ -8,7 +8,7 @@ import kotlin.contracts.contract
|
|||||||
*
|
*
|
||||||
* - Rust: [Result.and](https://doc.rust-lang.org/std/result/enum.Result.html#method.and)
|
* - Rust: [Result.and](https://doc.rust-lang.org/std/result/enum.Result.html#method.and)
|
||||||
*/
|
*/
|
||||||
infix fun <V, E> Result<V, E>.and(result: Result<V, E>): Result<V, E> {
|
public infix fun <V, E> Result<V, E>.and(result: Result<V, E>): Result<V, E> {
|
||||||
return when (this) {
|
return when (this) {
|
||||||
is Ok -> result
|
is Ok -> result
|
||||||
is Err -> this
|
is Err -> this
|
||||||
@ -16,7 +16,7 @@ infix fun <V, E> Result<V, E>.and(result: Result<V, E>): Result<V, E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Use andThen instead", ReplaceWith("andThen { result() }"))
|
@Deprecated("Use andThen instead", ReplaceWith("andThen { result() }"))
|
||||||
inline infix fun <V, E> Result<V, E>.and(result: () -> Result<V, E>): Result<V, E> {
|
public inline infix fun <V, E> Result<V, E>.and(result: () -> Result<V, E>): Result<V, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(result, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(result, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ inline infix fun <V, E> Result<V, E>.and(result: () -> Result<V, E>): Result<V,
|
|||||||
* - Elm: [Result.andThen](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#andThen)
|
* - Elm: [Result.andThen](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#andThen)
|
||||||
* - Rust: [Result.and_then](https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then)
|
* - Rust: [Result.and_then](https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then)
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E, U> Result<V, E>.andThen(transform: (V) -> Result<U, E>): Result<U, E> {
|
public inline infix fun <V, E, U> Result<V, E>.andThen(transform: (V) -> Result<U, E>): Result<U, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ import kotlin.contracts.contract
|
|||||||
*
|
*
|
||||||
* @sample com.github.michaelbull.result.BindingTest
|
* @sample com.github.michaelbull.result.BindingTest
|
||||||
*/
|
*/
|
||||||
inline fun <V, E> binding(crossinline block: ResultBinding<E>.() -> V): Result<V, E> {
|
public inline fun <V, E> binding(crossinline block: ResultBinding<E>.() -> V): Result<V, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||||
}
|
}
|
||||||
@ -40,8 +40,8 @@ inline fun <V, E> binding(crossinline block: ResultBinding<E>.() -> V): Result<V
|
|||||||
|
|
||||||
internal expect object BindException : Exception
|
internal expect object BindException : Exception
|
||||||
|
|
||||||
interface ResultBinding<E> {
|
public interface ResultBinding<E> {
|
||||||
fun <V> Result<V, E>.bind(): V
|
public fun <V> Result<V, E>.bind(): V
|
||||||
}
|
}
|
||||||
|
|
||||||
@PublishedApi
|
@PublishedApi
|
||||||
|
@ -8,7 +8,7 @@ import kotlin.contracts.contract
|
|||||||
* invocation was successful, catching and encapsulating any thrown exception
|
* invocation was successful, catching and encapsulating any thrown exception
|
||||||
* as a failure.
|
* as a failure.
|
||||||
*/
|
*/
|
||||||
inline fun <V> runCatching(block: () -> V): Result<V, Throwable> {
|
public inline fun <V> runCatching(block: () -> V): Result<V, Throwable> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||||
}
|
}
|
||||||
@ -25,7 +25,7 @@ inline fun <V> runCatching(block: () -> V): Result<V, Throwable> {
|
|||||||
* returns its encapsulated result if invocation was successful, catching and
|
* returns its encapsulated result if invocation was successful, catching and
|
||||||
* encapsulating any thrown exception as a failure.
|
* encapsulating any thrown exception as a failure.
|
||||||
*/
|
*/
|
||||||
inline infix fun <T, V> T.runCatching(block: T.() -> V): Result<V, Throwable> {
|
public inline infix fun <T, V> T.runCatching(block: T.() -> V): Result<V, Throwable> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||||
}
|
}
|
||||||
@ -41,7 +41,7 @@ inline infix fun <T, V> T.runCatching(block: T.() -> V): Result<V, Throwable> {
|
|||||||
* Converts a nullable of type [V] to a [Result]. Returns [Ok] if the value is
|
* Converts a nullable of type [V] to a [Result]. Returns [Ok] if the value is
|
||||||
* non-null, otherwise the supplied [error].
|
* non-null, otherwise the supplied [error].
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E> V?.toResultOr(error: () -> E): Result<V, E> {
|
public inline infix fun <V, E> V?.toResultOr(error: () -> E): Result<V, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(error, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(error, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import kotlin.contracts.contract
|
|||||||
* - Elm: [Result.toMaybe](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#toMaybe)
|
* - Elm: [Result.toMaybe](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#toMaybe)
|
||||||
* - Rust: [Result.ok](https://doc.rust-lang.org/std/result/enum.Result.html#method.ok)
|
* - Rust: [Result.ok](https://doc.rust-lang.org/std/result/enum.Result.html#method.ok)
|
||||||
*/
|
*/
|
||||||
fun <V, E> Result<V, E>.get(): V? {
|
public fun <V, E> Result<V, E>.get(): V? {
|
||||||
contract {
|
contract {
|
||||||
returnsNotNull() implies (this@get is Ok<V>)
|
returnsNotNull() implies (this@get is Ok<V>)
|
||||||
returns(null) implies (this@get is Err<E>)
|
returns(null) implies (this@get is Err<E>)
|
||||||
@ -26,7 +26,7 @@ fun <V, E> Result<V, E>.get(): V? {
|
|||||||
*
|
*
|
||||||
* - Rust: [Result.err](https://doc.rust-lang.org/std/result/enum.Result.html#method.err)
|
* - Rust: [Result.err](https://doc.rust-lang.org/std/result/enum.Result.html#method.err)
|
||||||
*/
|
*/
|
||||||
fun <V, E> Result<V, E>.getError(): E? {
|
public fun <V, E> Result<V, E>.getError(): E? {
|
||||||
contract {
|
contract {
|
||||||
returns(null) implies (this@getError is Ok<V>)
|
returns(null) implies (this@getError is Ok<V>)
|
||||||
returnsNotNull() implies (this@getError is Err<E>)
|
returnsNotNull() implies (this@getError is Err<E>)
|
||||||
@ -48,7 +48,7 @@ fun <V, E> Result<V, E>.getError(): E? {
|
|||||||
* @param default The value to return if [Err].
|
* @param default The value to return if [Err].
|
||||||
* @return The [value][Ok.value] if [Ok], otherwise [default].
|
* @return The [value][Ok.value] if [Ok], otherwise [default].
|
||||||
*/
|
*/
|
||||||
infix fun <V, E> Result<V, E>.getOr(default: V): V {
|
public infix fun <V, E> Result<V, E>.getOr(default: V): V {
|
||||||
return when (this) {
|
return when (this) {
|
||||||
is Ok -> value
|
is Ok -> value
|
||||||
is Err -> default
|
is Err -> default
|
||||||
@ -56,7 +56,7 @@ infix fun <V, E> Result<V, E>.getOr(default: V): V {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Use getOrElse instead", ReplaceWith("getOrElse { default() }"))
|
@Deprecated("Use getOrElse instead", ReplaceWith("getOrElse { default() }"))
|
||||||
inline infix fun <V, E> Result<V, E>.getOr(default: () -> V): V {
|
public inline infix fun <V, E> Result<V, E>.getOr(default: () -> V): V {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(default, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(default, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
@ -72,7 +72,7 @@ inline infix fun <V, E> Result<V, E>.getOr(default: () -> V): V {
|
|||||||
* @param default The error to return if [Ok].
|
* @param default The error to return if [Ok].
|
||||||
* @return The [error][Err.error] if [Err], otherwise [default].
|
* @return The [error][Err.error] if [Err], otherwise [default].
|
||||||
*/
|
*/
|
||||||
infix fun <V, E> Result<V, E>.getErrorOr(default: E): E {
|
public infix fun <V, E> Result<V, E>.getErrorOr(default: E): E {
|
||||||
return when (this) {
|
return when (this) {
|
||||||
is Ok -> default
|
is Ok -> default
|
||||||
is Err -> error
|
is Err -> error
|
||||||
@ -80,7 +80,7 @@ infix fun <V, E> Result<V, E>.getErrorOr(default: E): E {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Use getOrElse instead", ReplaceWith("getErrorOrElse { default() }"))
|
@Deprecated("Use getOrElse instead", ReplaceWith("getErrorOrElse { default() }"))
|
||||||
inline infix fun <V, E> Result<V, E>.getErrorOr(default: () -> E): E {
|
public inline infix fun <V, E> Result<V, E>.getErrorOr(default: () -> E): E {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(default, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(default, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
@ -95,7 +95,7 @@ inline infix fun <V, E> Result<V, E>.getErrorOr(default: () -> E): E {
|
|||||||
* - Elm: [Result.extract](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#extract)
|
* - Elm: [Result.extract](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#extract)
|
||||||
* - Rust: [Result.unwrap_or_else](https://doc.rust-lang.org/src/core/result.rs.html#735-740)
|
* - Rust: [Result.unwrap_or_else](https://doc.rust-lang.org/src/core/result.rs.html#735-740)
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E> Result<V, E>.getOrElse(transform: (E) -> V): V {
|
public inline infix fun <V, E> Result<V, E>.getOrElse(transform: (E) -> V): V {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
@ -110,7 +110,7 @@ inline infix fun <V, E> Result<V, E>.getOrElse(transform: (E) -> V): V {
|
|||||||
* Returns the [error][Err.error] if this [Result] is [Err], otherwise the
|
* Returns the [error][Err.error] if this [Result] is [Err], otherwise the
|
||||||
* [transformation][transform] of the [value][Ok.value].
|
* [transformation][transform] of the [value][Ok.value].
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E> Result<V, E>.getErrorOrElse(transform: (V) -> E): E {
|
public inline infix fun <V, E> Result<V, E>.getErrorOrElse(transform: (V) -> E): E {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
@ -127,7 +127,7 @@ inline infix fun <V, E> Result<V, E>.getErrorOrElse(transform: (V) -> E): E {
|
|||||||
*
|
*
|
||||||
* - Scala: [MergeableEither.merge](https://www.scala-lang.org/api/2.12.0/scala/util/Either$$MergeableEither.html#merge:A)
|
* - Scala: [MergeableEither.merge](https://www.scala-lang.org/api/2.12.0/scala/util/Either$$MergeableEither.html#merge:A)
|
||||||
*/
|
*/
|
||||||
fun <V : U, E : U, U> Result<V, E>.merge(): U {
|
public fun <V : U, E : U, U> Result<V, E>.merge(): U {
|
||||||
return when (this) {
|
return when (this) {
|
||||||
is Ok -> value
|
is Ok -> value
|
||||||
is Err -> error
|
is Err -> error
|
||||||
|
@ -4,7 +4,7 @@ package com.github.michaelbull.result
|
|||||||
* Accumulates value starting with [initial] value and applying [operation] from left to right to
|
* Accumulates value starting with [initial] value and applying [operation] from left to right to
|
||||||
* current accumulator value and each element.
|
* current accumulator value and each element.
|
||||||
*/
|
*/
|
||||||
inline fun <T, R, E> Iterable<T>.fold(initial: R, operation: (acc: R, T) -> Result<R, E>): Result<R, E> {
|
public inline fun <T, R, E> Iterable<T>.fold(initial: R, operation: (acc: R, T) -> Result<R, E>): Result<R, E> {
|
||||||
var accumulator = initial
|
var accumulator = initial
|
||||||
|
|
||||||
for (element in this) {
|
for (element in this) {
|
||||||
@ -21,7 +21,7 @@ inline fun <T, R, E> Iterable<T>.fold(initial: R, operation: (acc: R, T) -> Resu
|
|||||||
* Accumulates value starting with [initial] value and applying [operation] from right to left to
|
* Accumulates value starting with [initial] value and applying [operation] from right to left to
|
||||||
* each element and current accumulator value.
|
* each element and current accumulator value.
|
||||||
*/
|
*/
|
||||||
inline fun <T, R, E> List<T>.foldRight(initial: R, operation: (T, acc: R) -> Result<R, E>): Result<R, E> {
|
public inline fun <T, R, E> List<T>.foldRight(initial: R, operation: (T, acc: R) -> Result<R, E>): Result<R, E> {
|
||||||
var accumulator = initial
|
var accumulator = initial
|
||||||
|
|
||||||
if (!isEmpty()) {
|
if (!isEmpty()) {
|
||||||
@ -42,7 +42,7 @@ inline fun <T, R, E> List<T>.foldRight(initial: R, operation: (T, acc: R) -> Res
|
|||||||
*
|
*
|
||||||
* - Elm: [Result.Extra.combine](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#combine)
|
* - Elm: [Result.Extra.combine](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#combine)
|
||||||
*/
|
*/
|
||||||
fun <V, E> combine(vararg results: Result<V, E>): Result<List<V>, E> {
|
public fun <V, E> combine(vararg results: Result<V, E>): Result<List<V>, E> {
|
||||||
return results.asIterable().combine()
|
return results.asIterable().combine()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ fun <V, E> combine(vararg results: Result<V, E>): Result<List<V>, E> {
|
|||||||
*
|
*
|
||||||
* - Elm: [Result.Extra.combine](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#combine)
|
* - Elm: [Result.Extra.combine](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#combine)
|
||||||
*/
|
*/
|
||||||
fun <V, E> Iterable<Result<V, E>>.combine(): Result<List<V>, E> {
|
public fun <V, E> Iterable<Result<V, E>>.combine(): Result<List<V>, E> {
|
||||||
return Ok(map {
|
return Ok(map {
|
||||||
when (it) {
|
when (it) {
|
||||||
is Ok -> it.value
|
is Ok -> it.value
|
||||||
@ -66,7 +66,7 @@ fun <V, E> Iterable<Result<V, E>>.combine(): Result<List<V>, E> {
|
|||||||
*
|
*
|
||||||
* - Haskell: [Data.Either.lefts](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:lefts)
|
* - Haskell: [Data.Either.lefts](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:lefts)
|
||||||
*/
|
*/
|
||||||
fun <V, E> getAll(vararg results: Result<V, E>): List<V> {
|
public fun <V, E> getAll(vararg results: Result<V, E>): List<V> {
|
||||||
return results.asIterable().getAll()
|
return results.asIterable().getAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ fun <V, E> getAll(vararg results: Result<V, E>): List<V> {
|
|||||||
*
|
*
|
||||||
* - Haskell: [Data.Either.lefts](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:lefts)
|
* - Haskell: [Data.Either.lefts](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:lefts)
|
||||||
*/
|
*/
|
||||||
fun <V, E> Iterable<Result<V, E>>.getAll(): List<V> {
|
public fun <V, E> Iterable<Result<V, E>>.getAll(): List<V> {
|
||||||
return filterIsInstance<Ok<V>>().map { it.value }
|
return filterIsInstance<Ok<V>>().map { it.value }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ fun <V, E> Iterable<Result<V, E>>.getAll(): List<V> {
|
|||||||
*
|
*
|
||||||
* - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights)
|
* - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights)
|
||||||
*/
|
*/
|
||||||
fun <V, E> getAllErrors(vararg results: Result<V, E>) = results.asIterable().getAllErrors()
|
public fun <V, E> getAllErrors(vararg results: Result<V, E>): List<E> = results.asIterable().getAllErrors()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts from an [Iterable] of [Results][Result] all the [Err] elements. All the [Err] elements
|
* Extracts from an [Iterable] of [Results][Result] all the [Err] elements. All the [Err] elements
|
||||||
@ -94,7 +94,7 @@ fun <V, E> getAllErrors(vararg results: Result<V, E>) = results.asIterable().get
|
|||||||
*
|
*
|
||||||
* - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights)
|
* - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights)
|
||||||
*/
|
*/
|
||||||
fun <V, E> Iterable<Result<V, E>>.getAllErrors(): List<E> {
|
public fun <V, E> Iterable<Result<V, E>>.getAllErrors(): List<E> {
|
||||||
return filterIsInstance<Err<E>>().map { it.error }
|
return filterIsInstance<Err<E>>().map { it.error }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +105,7 @@ fun <V, E> Iterable<Result<V, E>>.getAllErrors(): List<E> {
|
|||||||
*
|
*
|
||||||
* - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers)
|
* - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers)
|
||||||
*/
|
*/
|
||||||
fun <V, E> partition(vararg results: Result<V, E>): Pair<List<V>, List<E>> {
|
public fun <V, E> partition(vararg results: Result<V, E>): Pair<List<V>, List<E>> {
|
||||||
return results.asIterable().partition()
|
return results.asIterable().partition()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,7 +116,7 @@ fun <V, E> partition(vararg results: Result<V, E>): Pair<List<V>, List<E>> {
|
|||||||
*
|
*
|
||||||
* - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers)
|
* - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers)
|
||||||
*/
|
*/
|
||||||
fun <V, E> Iterable<Result<V, E>>.partition(): Pair<List<V>, List<E>> {
|
public fun <V, E> Iterable<Result<V, E>>.partition(): Pair<List<V>, List<E>> {
|
||||||
val values = mutableListOf<V>()
|
val values = mutableListOf<V>()
|
||||||
val errors = mutableListOf<E>()
|
val errors = mutableListOf<E>()
|
||||||
|
|
||||||
@ -135,7 +135,7 @@ fun <V, E> Iterable<Result<V, E>>.partition(): Pair<List<V>, List<E>> {
|
|||||||
* function to each element in the original collection, returning early with the first [Err] if a
|
* function to each element in the original collection, returning early with the first [Err] if a
|
||||||
* transformation fails.
|
* transformation fails.
|
||||||
*/
|
*/
|
||||||
inline fun <V, E, U> Iterable<V>.mapResult(
|
public inline fun <V, E, U> Iterable<V>.mapResult(
|
||||||
transform: (V) -> Result<U, E>
|
transform: (V) -> Result<U, E>
|
||||||
): Result<List<U>, E> {
|
): Result<List<U>, E> {
|
||||||
return Ok(map { element ->
|
return Ok(map { element ->
|
||||||
@ -151,7 +151,7 @@ inline fun <V, E, U> Iterable<V>.mapResult(
|
|||||||
* the results to the given [destination], returning early with the first [Err] if a
|
* the results to the given [destination], returning early with the first [Err] if a
|
||||||
* transformation fails.
|
* transformation fails.
|
||||||
*/
|
*/
|
||||||
inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultTo(
|
public inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultTo(
|
||||||
destination: C,
|
destination: C,
|
||||||
transform: (V) -> Result<U, E>
|
transform: (V) -> Result<U, E>
|
||||||
): Result<C, E> {
|
): Result<C, E> {
|
||||||
@ -168,7 +168,7 @@ inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultTo(
|
|||||||
* given [transform] function to each element in the original collection, returning early with the
|
* given [transform] function to each element in the original collection, returning early with the
|
||||||
* first [Err] if a transformation fails.
|
* first [Err] if a transformation fails.
|
||||||
*/
|
*/
|
||||||
inline fun <V, E, U : Any> Iterable<V>.mapResultNotNull(
|
public inline fun <V, E, U : Any> Iterable<V>.mapResultNotNull(
|
||||||
transform: (V) -> Result<U, E>?
|
transform: (V) -> Result<U, E>?
|
||||||
): Result<List<U>, E> {
|
): Result<List<U>, E> {
|
||||||
return Ok(mapNotNull { element ->
|
return Ok(mapNotNull { element ->
|
||||||
@ -185,7 +185,7 @@ inline fun <V, E, U : Any> Iterable<V>.mapResultNotNull(
|
|||||||
* only the non-null results to the given [destination], returning early with the first [Err] if a
|
* only the non-null results to the given [destination], returning early with the first [Err] if a
|
||||||
* transformation fails.
|
* transformation fails.
|
||||||
*/
|
*/
|
||||||
inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultNotNullTo(
|
public inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultNotNullTo(
|
||||||
destination: C,
|
destination: C,
|
||||||
transform: (V) -> Result<U, E>?
|
transform: (V) -> Result<U, E>?
|
||||||
): Result<C, E> {
|
): Result<C, E> {
|
||||||
@ -203,7 +203,7 @@ inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultNot
|
|||||||
* function to each element and its index in the original collection, returning early with the
|
* function to each element and its index in the original collection, returning early with the
|
||||||
* first [Err] if a transformation fails.
|
* first [Err] if a transformation fails.
|
||||||
*/
|
*/
|
||||||
inline fun <V, E, U> Iterable<V>.mapResultIndexed(
|
public inline fun <V, E, U> Iterable<V>.mapResultIndexed(
|
||||||
transform: (index: Int, V) -> Result<U, E>
|
transform: (index: Int, V) -> Result<U, E>
|
||||||
): Result<List<U>, E> {
|
): Result<List<U>, E> {
|
||||||
return Ok(mapIndexed { index, element ->
|
return Ok(mapIndexed { index, element ->
|
||||||
@ -219,7 +219,7 @@ inline fun <V, E, U> Iterable<V>.mapResultIndexed(
|
|||||||
* and appends the results to the given [destination], returning early with the first [Err] if a
|
* and appends the results to the given [destination], returning early with the first [Err] if a
|
||||||
* transformation fails.
|
* transformation fails.
|
||||||
*/
|
*/
|
||||||
inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedTo(
|
public inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedTo(
|
||||||
destination: C,
|
destination: C,
|
||||||
transform: (index: Int, V) -> Result<U, E>
|
transform: (index: Int, V) -> Result<U, E>
|
||||||
): Result<C, E> {
|
): Result<C, E> {
|
||||||
@ -236,7 +236,7 @@ inline fun <V, E, U, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedTo
|
|||||||
* given [transform] function to each element and its index in the original collection, returning
|
* given [transform] function to each element and its index in the original collection, returning
|
||||||
* early with the first [Err] if a transformation fails.
|
* early with the first [Err] if a transformation fails.
|
||||||
*/
|
*/
|
||||||
inline fun <V, E, U : Any> Iterable<V>.mapResultIndexedNotNull(
|
public inline fun <V, E, U : Any> Iterable<V>.mapResultIndexedNotNull(
|
||||||
transform: (index: Int, V) -> Result<U, E>?
|
transform: (index: Int, V) -> Result<U, E>?
|
||||||
): Result<List<U>, E> {
|
): Result<List<U>, E> {
|
||||||
return Ok(mapIndexedNotNull { index, element ->
|
return Ok(mapIndexedNotNull { index, element ->
|
||||||
@ -253,7 +253,7 @@ inline fun <V, E, U : Any> Iterable<V>.mapResultIndexedNotNull(
|
|||||||
* and appends only the non-null results to the given [destination], returning early with the first
|
* and appends only the non-null results to the given [destination], returning early with the first
|
||||||
* [Err] if a transformation fails.
|
* [Err] if a transformation fails.
|
||||||
*/
|
*/
|
||||||
inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedNotNullTo(
|
public inline fun <V, E, U : Any, C : MutableCollection<in U>> Iterable<V>.mapResultIndexedNotNullTo(
|
||||||
destination: C,
|
destination: C,
|
||||||
transform: (index: Int, V) -> Result<U, E>?
|
transform: (index: Int, V) -> Result<U, E>?
|
||||||
): Result<C, E> {
|
): Result<C, E> {
|
||||||
|
@ -11,7 +11,7 @@ import kotlin.contracts.contract
|
|||||||
* - Haskell: [Data.Bifunctor.first](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:first)
|
* - Haskell: [Data.Bifunctor.first](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:first)
|
||||||
* - Rust: [Result.map](https://doc.rust-lang.org/std/result/enum.Result.html#method.map)
|
* - Rust: [Result.map](https://doc.rust-lang.org/std/result/enum.Result.html#method.map)
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E, U> Result<V, E>.map(transform: (V) -> U): Result<U, E> {
|
public inline infix fun <V, E, U> Result<V, E>.map(transform: (V) -> U): Result<U, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
@ -30,7 +30,7 @@ inline infix fun <V, E, U> Result<V, E>.map(transform: (V) -> U): Result<U, E> {
|
|||||||
* - Haskell: [Data.Bifunctor.right](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:second)
|
* - Haskell: [Data.Bifunctor.right](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:second)
|
||||||
* - Rust: [Result.map_err](https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err)
|
* - Rust: [Result.map_err](https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err)
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E, F> Result<V, E>.mapError(transform: (E) -> F): Result<V, F> {
|
public inline infix fun <V, E, F> Result<V, E>.mapError(transform: (E) -> F): Result<V, F> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
@ -48,7 +48,7 @@ inline infix fun <V, E, F> Result<V, E>.mapError(transform: (E) -> F): Result<V,
|
|||||||
*
|
*
|
||||||
* - Rust: [Result.map_or](https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or)
|
* - Rust: [Result.map_or](https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or)
|
||||||
*/
|
*/
|
||||||
inline fun <V, E, U> Result<V, E>.mapOr(default: U, transform: (V) -> U): U {
|
public inline fun <V, E, U> Result<V, E>.mapOr(default: U, transform: (V) -> U): U {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
@ -66,7 +66,7 @@ inline fun <V, E, U> Result<V, E>.mapOr(default: U, transform: (V) -> U): U {
|
|||||||
*
|
*
|
||||||
* - Rust: [Result.map_or_else](https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or_else)
|
* - Rust: [Result.map_or_else](https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or_else)
|
||||||
*/
|
*/
|
||||||
inline fun <V, E, U> Result<V, E>.mapOrElse(default: (E) -> U, transform: (V) -> U): U {
|
public inline fun <V, E, U> Result<V, E>.mapOrElse(default: (E) -> U, transform: (V) -> U): U {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(default, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(default, InvocationKind.AT_MOST_ONCE)
|
||||||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
||||||
@ -83,7 +83,7 @@ inline fun <V, E, U> Result<V, E>.mapOrElse(default: (E) -> U, transform: (V) ->
|
|||||||
* function to each element in the original collection, returning early with the first [Err] if a
|
* function to each element in the original collection, returning early with the first [Err] if a
|
||||||
* transformation fails.
|
* transformation fails.
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E, U> Result<Iterable<V>, E>.mapAll(transform: (V) -> Result<U, E>): Result<List<U>, E> {
|
public inline infix fun <V, E, U> Result<Iterable<V>, E>.mapAll(transform: (V) -> Result<U, E>): Result<List<U>, E> {
|
||||||
return map { iterable ->
|
return map { iterable ->
|
||||||
iterable.map { element ->
|
iterable.map { element ->
|
||||||
when (val transformed = transform(element)) {
|
when (val transformed = transform(element)) {
|
||||||
@ -102,7 +102,7 @@ inline infix fun <V, E, U> Result<Iterable<V>, E>.mapAll(transform: (V) -> Resul
|
|||||||
* - Elm: [Result.Extra.mapBoth](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#mapBoth)
|
* - Elm: [Result.Extra.mapBoth](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#mapBoth)
|
||||||
* - Haskell: [Data.Either.either](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:either)
|
* - Haskell: [Data.Either.either](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:either)
|
||||||
*/
|
*/
|
||||||
inline fun <V, E, U> Result<V, E>.mapBoth(success: (V) -> U, failure: (E) -> U): U {
|
public inline fun <V, E, U> Result<V, E>.mapBoth(success: (V) -> U, failure: (E) -> U): U {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(success, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(success, InvocationKind.AT_MOST_ONCE)
|
||||||
callsInPlace(failure, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(failure, InvocationKind.AT_MOST_ONCE)
|
||||||
@ -124,7 +124,7 @@ inline fun <V, E, U> Result<V, E>.mapBoth(success: (V) -> U, failure: (E) -> U):
|
|||||||
* - Elm: [Result.Extra.mapBoth](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#mapBoth)
|
* - Elm: [Result.Extra.mapBoth](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#mapBoth)
|
||||||
* - Haskell: [Data.Either.either](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:either)
|
* - Haskell: [Data.Either.either](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:either)
|
||||||
*/
|
*/
|
||||||
inline fun <V, E, U> Result<V, E>.fold(success: (V) -> U, failure: (E) -> U): U {
|
public inline fun <V, E, U> Result<V, E>.fold(success: (V) -> U, failure: (E) -> U): U {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(success, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(success, InvocationKind.AT_MOST_ONCE)
|
||||||
callsInPlace(failure, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(failure, InvocationKind.AT_MOST_ONCE)
|
||||||
@ -139,7 +139,7 @@ inline fun <V, E, U> Result<V, E>.fold(success: (V) -> U, failure: (E) -> U): U
|
|||||||
*
|
*
|
||||||
* - Haskell: [Data.Bifunctor.Bimap](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:bimap)
|
* - Haskell: [Data.Bifunctor.Bimap](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:bimap)
|
||||||
*/
|
*/
|
||||||
inline fun <V, E, U, F> Result<V, E>.mapEither(success: (V) -> U, failure: (E) -> F): Result<U, F> {
|
public inline fun <V, E, U, F> Result<V, E>.mapEither(success: (V) -> U, failure: (E) -> F): Result<U, F> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(success, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(success, InvocationKind.AT_MOST_ONCE)
|
||||||
callsInPlace(failure, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(failure, InvocationKind.AT_MOST_ONCE)
|
||||||
@ -159,7 +159,7 @@ inline fun <V, E, U, F> Result<V, E>.mapEither(success: (V) -> U, failure: (E) -
|
|||||||
*
|
*
|
||||||
* - Scala: [Either.flatMap](http://www.scala-lang.org/api/2.12.0/scala/util/Either.html#flatMap[AA>:A,Y](f:B=>scala.util.Either[AA,Y]):scala.util.Either[AA,Y])
|
* - Scala: [Either.flatMap](http://www.scala-lang.org/api/2.12.0/scala/util/Either.html#flatMap[AA>:A,Y](f:B=>scala.util.Either[AA,Y]):scala.util.Either[AA,Y])
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E, U> Result<V, E>.flatMap(transform: (V) -> Result<U, E>): Result<U, E> {
|
public inline infix fun <V, E, U> Result<V, E>.flatMap(transform: (V) -> Result<U, E>): Result<U, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
@ -173,7 +173,7 @@ inline infix fun <V, E, U> Result<V, E>.flatMap(transform: (V) -> Result<U, E>):
|
|||||||
*
|
*
|
||||||
* @see [takeIf]
|
* @see [takeIf]
|
||||||
*/
|
*/
|
||||||
inline fun <V, E> Result<V, E>.toErrorIf(predicate: (V) -> Boolean, transform: (V) -> E): Result<V, E> {
|
public inline fun <V, E> Result<V, E>.toErrorIf(predicate: (V) -> Boolean, transform: (V) -> E): Result<V, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE)
|
||||||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
||||||
@ -195,7 +195,7 @@ inline fun <V, E> Result<V, E>.toErrorIf(predicate: (V) -> Boolean, transform: (
|
|||||||
*
|
*
|
||||||
* @see [takeUnless]
|
* @see [takeUnless]
|
||||||
*/
|
*/
|
||||||
inline fun <V, E> Result<V, E>.toErrorUnless(predicate: (V) -> Boolean, transform: (V) -> E): Result<V, E> {
|
public inline fun <V, E> Result<V, E>.toErrorUnless(predicate: (V) -> Boolean, transform: (V) -> E): Result<V, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE)
|
||||||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
||||||
|
@ -6,7 +6,7 @@ import kotlin.contracts.contract
|
|||||||
/**
|
/**
|
||||||
* Invokes an [action] if this [Result] is [Ok].
|
* Invokes an [action] if this [Result] is [Ok].
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E> Result<V, E>.onSuccess(action: (V) -> Unit): Result<V, E> {
|
public inline infix fun <V, E> Result<V, E>.onSuccess(action: (V) -> Unit): Result<V, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
@ -21,7 +21,7 @@ inline infix fun <V, E> Result<V, E>.onSuccess(action: (V) -> Unit): Result<V, E
|
|||||||
/**
|
/**
|
||||||
* Invokes an [action] if this [Result] is [Err].
|
* Invokes an [action] if this [Result] is [Err].
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E> Result<V, E>.onFailure(action: (E) -> Unit): Result<V, E> {
|
public inline infix fun <V, E> Result<V, E>.onFailure(action: (E) -> Unit): Result<V, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import kotlin.contracts.contract
|
|||||||
*
|
*
|
||||||
* - Rust: [Result.or](https://doc.rust-lang.org/std/result/enum.Result.html#method.or)
|
* - Rust: [Result.or](https://doc.rust-lang.org/std/result/enum.Result.html#method.or)
|
||||||
*/
|
*/
|
||||||
infix fun <V, E> Result<V, E>.or(result: Result<V, E>): Result<V, E> {
|
public infix fun <V, E> Result<V, E>.or(result: Result<V, E>): Result<V, E> {
|
||||||
return when (this) {
|
return when (this) {
|
||||||
is Ok -> this
|
is Ok -> this
|
||||||
is Err -> result
|
is Err -> result
|
||||||
@ -16,7 +16,7 @@ infix fun <V, E> Result<V, E>.or(result: Result<V, E>): Result<V, E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Use orElse instead", ReplaceWith("orElse { result() }"))
|
@Deprecated("Use orElse instead", ReplaceWith("orElse { result() }"))
|
||||||
inline infix fun <V, E> Result<V, E>.or(result: () -> Result<V, E>): Result<V, E> {
|
public inline infix fun <V, E> Result<V, E>.or(result: () -> Result<V, E>): Result<V, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(result, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(result, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
@ -30,7 +30,7 @@ inline infix fun <V, E> Result<V, E>.or(result: () -> Result<V, E>): Result<V, E
|
|||||||
*
|
*
|
||||||
* - Rust: [Result.or_else](https://doc.rust-lang.org/std/result/enum.Result.html#method.or_else)
|
* - Rust: [Result.or_else](https://doc.rust-lang.org/std/result/enum.Result.html#method.or_else)
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E> Result<V, E>.orElse(transform: (E) -> Result<V, E>): Result<V, E> {
|
public inline infix fun <V, E> Result<V, E>.orElse(transform: (E) -> Result<V, E>): Result<V, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
@ -45,7 +45,7 @@ inline infix fun <V, E> Result<V, E>.orElse(transform: (E) -> Result<V, E>): Res
|
|||||||
* Returns the [transformation][transform] of the [error][Err.error] if this [Result] is [Err],
|
* Returns the [transformation][transform] of the [error][Err.error] if this [Result] is [Err],
|
||||||
* otherwise this [Ok].
|
* otherwise this [Ok].
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E> Result<V, E>.recover(transform: (E) -> V): Ok<V> {
|
public inline infix fun <V, E> Result<V, E>.recover(transform: (E) -> V): Ok<V> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
|
||||||
}
|
}
|
||||||
|
@ -7,19 +7,19 @@ package com.github.michaelbull.result
|
|||||||
* - Haskell: [Data.Either](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html)
|
* - Haskell: [Data.Either](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html)
|
||||||
* - Rust: [Result](https://doc.rust-lang.org/std/result/enum.Result.html)
|
* - Rust: [Result](https://doc.rust-lang.org/std/result/enum.Result.html)
|
||||||
*/
|
*/
|
||||||
sealed class Result<out V, out E> {
|
public sealed class Result<out V, out E> {
|
||||||
|
|
||||||
abstract operator fun component1(): V?
|
public abstract operator fun component1(): V?
|
||||||
abstract operator fun component2(): E?
|
public abstract operator fun component2(): E?
|
||||||
|
|
||||||
companion object {
|
public companion object {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invokes a [function] and wraps it in a [Result], returning an [Err]
|
* Invokes a [function] and wraps it in a [Result], returning an [Err]
|
||||||
* if an [Exception] was thrown, otherwise [Ok].
|
* if an [Exception] was thrown, otherwise [Ok].
|
||||||
*/
|
*/
|
||||||
@Deprecated("Use top-level runCatching instead", ReplaceWith("runCatching(function)"))
|
@Deprecated("Use top-level runCatching instead", ReplaceWith("runCatching(function)"))
|
||||||
inline fun <V> of(function: () -> V): Result<V, Exception> {
|
public inline fun <V> of(function: () -> V): Result<V, Exception> {
|
||||||
return try {
|
return try {
|
||||||
Ok(function.invoke())
|
Ok(function.invoke())
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
@ -32,10 +32,10 @@ sealed class Result<out V, out E> {
|
|||||||
/**
|
/**
|
||||||
* Represents a successful [Result], containing a [value].
|
* Represents a successful [Result], containing a [value].
|
||||||
*/
|
*/
|
||||||
class Ok<out V>(val value: V) : Result<V, Nothing>() {
|
public class Ok<out V>(public val value: V) : Result<V, Nothing>() {
|
||||||
|
|
||||||
override fun component1() = value
|
override fun component1(): V = value
|
||||||
override fun component2() = null
|
override fun component2(): Nothing? = null
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
override fun equals(other: Any?): Boolean {
|
||||||
if (this === other) return true
|
if (this === other) return true
|
||||||
@ -48,17 +48,17 @@ class Ok<out V>(val value: V) : Result<V, Nothing>() {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun hashCode() = value.hashCode()
|
override fun hashCode(): Int = value.hashCode()
|
||||||
override fun toString() = "Ok($value)"
|
override fun toString(): String = "Ok($value)"
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a failed [Result], containing an [error].
|
* Represents a failed [Result], containing an [error].
|
||||||
*/
|
*/
|
||||||
class Err<out E>(val error: E) : Result<Nothing, E>() {
|
public class Err<out E>(public val error: E) : Result<Nothing, E>() {
|
||||||
|
|
||||||
override fun component1() = null
|
override fun component1(): Nothing? = null
|
||||||
override fun component2() = error
|
override fun component2(): E = error
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
override fun equals(other: Any?): Boolean {
|
||||||
if (this === other) return true
|
if (this === other) return true
|
||||||
@ -71,6 +71,6 @@ class Err<out E>(val error: E) : Result<Nothing, E>() {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun hashCode() = error.hashCode()
|
override fun hashCode(): Int = error.hashCode()
|
||||||
override fun toString() = "Err($error)"
|
override fun toString(): String = "Err($error)"
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ package com.github.michaelbull.result
|
|||||||
*
|
*
|
||||||
* - Rust: [Result.iter](https://doc.rust-lang.org/std/result/enum.Result.html#method.iter)
|
* - Rust: [Result.iter](https://doc.rust-lang.org/std/result/enum.Result.html#method.iter)
|
||||||
*/
|
*/
|
||||||
fun <V, E> Result<V, E>.iterator(): Iterator<V> {
|
public fun <V, E> Result<V, E>.iterator(): Iterator<V> {
|
||||||
return ResultIterator(this)
|
return ResultIterator(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -16,7 +16,7 @@ fun <V, E> Result<V, E>.iterator(): Iterator<V> {
|
|||||||
*
|
*
|
||||||
* - Rust: [Result.iter_mut](https://doc.rust-lang.org/std/result/enum.Result.html#method.iter_mut)
|
* - Rust: [Result.iter_mut](https://doc.rust-lang.org/std/result/enum.Result.html#method.iter_mut)
|
||||||
*/
|
*/
|
||||||
fun <V, E> Result<V, E>.mutableIterator(): MutableIterator<V> {
|
public fun <V, E> Result<V, E>.mutableIterator(): MutableIterator<V> {
|
||||||
return ResultIterator(this)
|
return ResultIterator(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ package com.github.michaelbull.result
|
|||||||
import kotlin.contracts.InvocationKind
|
import kotlin.contracts.InvocationKind
|
||||||
import kotlin.contracts.contract
|
import kotlin.contracts.contract
|
||||||
|
|
||||||
class UnwrapException(message: String) : Exception(message)
|
public class UnwrapException(message: String) : Exception(message)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unwraps a [Result], yielding the [value][Ok.value].
|
* Unwraps a [Result], yielding the [value][Ok.value].
|
||||||
@ -12,7 +12,7 @@ class UnwrapException(message: String) : Exception(message)
|
|||||||
*
|
*
|
||||||
* @throws UnwrapException if the [Result] is an [Err], with a message containing the [error][Err.error].
|
* @throws UnwrapException if the [Result] is an [Err], with a message containing the [error][Err.error].
|
||||||
*/
|
*/
|
||||||
fun <V, E> Result<V, E>.unwrap(): V {
|
public fun <V, E> Result<V, E>.unwrap(): V {
|
||||||
contract {
|
contract {
|
||||||
returns() implies (this@unwrap is Ok<V>)
|
returns() implies (this@unwrap is Ok<V>)
|
||||||
}
|
}
|
||||||
@ -24,7 +24,7 @@ fun <V, E> Result<V, E>.unwrap(): V {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("expect { message }"))
|
@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("expect { message }"))
|
||||||
infix fun <V, E> Result<V, E>.expect(message: String): V {
|
public infix fun <V, E> Result<V, E>.expect(message: String): V {
|
||||||
contract {
|
contract {
|
||||||
returns() implies (this@expect is Ok<V>)
|
returns() implies (this@expect is Ok<V>)
|
||||||
}
|
}
|
||||||
@ -40,7 +40,7 @@ infix fun <V, E> Result<V, E>.expect(message: String): V {
|
|||||||
* @param message The message to include in the [UnwrapException] if the [Result] is an [Err].
|
* @param message The message to include in the [UnwrapException] if the [Result] is an [Err].
|
||||||
* @throws UnwrapException if the [Result] is an [Err], with the specified [message].
|
* @throws UnwrapException if the [Result] is an [Err], with the specified [message].
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E> Result<V, E>.expect(message: () -> Any): V {
|
public inline infix fun <V, E> Result<V, E>.expect(message: () -> Any): V {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(message, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(message, InvocationKind.AT_MOST_ONCE)
|
||||||
returns() implies (this@expect is Ok<V>)
|
returns() implies (this@expect is Ok<V>)
|
||||||
@ -59,7 +59,7 @@ inline infix fun <V, E> Result<V, E>.expect(message: () -> Any): V {
|
|||||||
*
|
*
|
||||||
* @throws UnwrapException if the [Result] is [Ok], with a message containing the [value][Ok.value].
|
* @throws UnwrapException if the [Result] is [Ok], with a message containing the [value][Ok.value].
|
||||||
*/
|
*/
|
||||||
fun <V, E> Result<V, E>.unwrapError(): E {
|
public fun <V, E> Result<V, E>.unwrapError(): E {
|
||||||
contract {
|
contract {
|
||||||
returns() implies (this@unwrapError is Err<E>)
|
returns() implies (this@unwrapError is Err<E>)
|
||||||
}
|
}
|
||||||
@ -71,7 +71,7 @@ fun <V, E> Result<V, E>.unwrapError(): E {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("expectError { message }"))
|
@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("expectError { message }"))
|
||||||
infix fun <V, E> Result<V, E>.expectError(message: String): E {
|
public infix fun <V, E> Result<V, E>.expectError(message: String): E {
|
||||||
contract {
|
contract {
|
||||||
returns() implies (this@expectError is Err<E>)
|
returns() implies (this@expectError is Err<E>)
|
||||||
}
|
}
|
||||||
@ -87,7 +87,7 @@ infix fun <V, E> Result<V, E>.expectError(message: String): E {
|
|||||||
* @param message The message to include in the [UnwrapException] if the [Result] is [Ok].
|
* @param message The message to include in the [UnwrapException] if the [Result] is [Ok].
|
||||||
* @throws UnwrapException if the [Result] is [Ok], with the specified [message].
|
* @throws UnwrapException if the [Result] is [Ok], with the specified [message].
|
||||||
*/
|
*/
|
||||||
inline infix fun <V, E> Result<V, E>.expectError(message: () -> Any): E {
|
public inline infix fun <V, E> Result<V, E>.expectError(message: () -> Any): E {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(message, InvocationKind.AT_MOST_ONCE)
|
callsInPlace(message, InvocationKind.AT_MOST_ONCE)
|
||||||
returns() implies (this@expectError is Err<E>)
|
returns() implies (this@expectError is Err<E>)
|
||||||
|
@ -11,7 +11,7 @@ private typealias Producer<T, E> = () -> Result<T, E>
|
|||||||
*
|
*
|
||||||
* - Elm: http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map2
|
* - Elm: http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map2
|
||||||
*/
|
*/
|
||||||
inline fun <T1, T2, E, V> zip(
|
public inline fun <T1, T2, E, V> zip(
|
||||||
result1: Producer<T1, E>,
|
result1: Producer<T1, E>,
|
||||||
result2: Producer<T2, E>,
|
result2: Producer<T2, E>,
|
||||||
transform: (T1, T2) -> V
|
transform: (T1, T2) -> V
|
||||||
@ -35,7 +35,7 @@ inline fun <T1, T2, E, V> zip(
|
|||||||
*
|
*
|
||||||
* - Elm: http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map3
|
* - Elm: http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map3
|
||||||
*/
|
*/
|
||||||
inline fun <T1, T2, T3, E, V> zip(
|
public inline fun <T1, T2, T3, E, V> zip(
|
||||||
result1: Producer<T1, E>,
|
result1: Producer<T1, E>,
|
||||||
result2: Producer<T2, E>,
|
result2: Producer<T2, E>,
|
||||||
result3: Producer<T3, E>,
|
result3: Producer<T3, E>,
|
||||||
@ -63,7 +63,7 @@ inline fun <T1, T2, T3, E, V> zip(
|
|||||||
*
|
*
|
||||||
* - Elm: http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map4
|
* - Elm: http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map4
|
||||||
*/
|
*/
|
||||||
inline fun <T1, T2, T3, T4, E, V> zip(
|
public inline fun <T1, T2, T3, T4, E, V> zip(
|
||||||
result1: Producer<T1, E>,
|
result1: Producer<T1, E>,
|
||||||
result2: Producer<T2, E>,
|
result2: Producer<T2, E>,
|
||||||
result3: Producer<T3, E>,
|
result3: Producer<T3, E>,
|
||||||
@ -95,7 +95,7 @@ inline fun <T1, T2, T3, T4, E, V> zip(
|
|||||||
*
|
*
|
||||||
* - Elm: http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map5
|
* - Elm: http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map5
|
||||||
*/
|
*/
|
||||||
inline fun <T1, T2, T3, T4, T5, E, V> zip(
|
public inline fun <T1, T2, T3, T4, T5, E, V> zip(
|
||||||
result1: Producer<T1, E>,
|
result1: Producer<T1, E>,
|
||||||
result2: Producer<T2, E>,
|
result2: Producer<T2, E>,
|
||||||
result3: Producer<T3, E>,
|
result3: Producer<T3, E>,
|
||||||
|
@ -17,7 +17,7 @@ import kotlin.contracts.contract
|
|||||||
"Please import the kotlin-result-coroutines library to continue using this feature.",
|
"Please import the kotlin-result-coroutines library to continue using this feature.",
|
||||||
level = DeprecationLevel.WARNING
|
level = DeprecationLevel.WARNING
|
||||||
)
|
)
|
||||||
suspend inline fun <V, E> binding(crossinline block: suspend ResultBinding<E>.() -> V): Result<V, E> {
|
public suspend inline fun <V, E> binding(crossinline block: suspend ResultBinding<E>.() -> V): Result<V, E> {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user