-
Notifications
You must be signed in to change notification settings - Fork 65
Scala
Michael Bull edited this page Aug 26, 2020
·
13 revisions
Mappings from Scala's Either
type to kotlin-result
.
<V, E, U, F> Result<V, E>.mapEither(success: (V) -> U, failure: (E) -> F): Result<U, F>
Applies success
if this is Ok
or failure
if this is an Err
.
val result = possiblyFailingOperation()
logger.info(result.mapEither(
success = { "Operation produced value: ${it.value}" },
failure = { "Operation failed with ${it.reason}" }
))
<V, E> Result<V, E>.getOr(default: V): V
Returns the value
from this Ok
or the given argument if this is an Err
.
Ok(12).getOr(17) // 12
Err(12).getOr(17) // 17
<V, E, U> Result<V, E>.andThen(transform: (V) -> Result<U, E>): Result<U, E>
The given function is applied if this is Ok
.
Ok(12).andThen { "flower" } // Result: Ok("flower")
Err(12).andThen { "flower" } // Result: Err(12)
<V, E> Result<V, E>.get(): V?
Returns the value if it exists or null
if this is an Err
.
<V : U, E : U, U> Result<V, E>.merge(): U
Allows use of a merge
method to extract values from Result
instances regardless of whether they are Ok
or Err
.