-
Notifications
You must be signed in to change notification settings - Fork 65
Rust
Mappings from Rust's Result
type to kotlin-result
.
Returns true
if the result is Ok
.
val result = Ok(500)
if (result is Ok) {
println("Result is ok")
}
Returns true
if the result is Err
.
val result = Err(500)
if (result is Err) {
println("Result is not ok")
}
<V, E> Result<V, E>.get(): V?
Converts from Result<V, E>
to V?
.
val value = Ok(230).get()
val error = Err("example").get()
// value = 230
// error = null
Converts from Result<V, E>
to E?
.
val value = Ok(230).getError()
val error = Err("example").getError()
// value = null
// error = "example"
<V, E, U> Result<V, E>.map(transform: (V) -> U): Result<U, E>
Maps a Result<V, E>
to Result<U, E>
by applying a function to a contained Ok
value, leaving an Err
value untouched.
This function can be used to compose the results of two functions.
val value = Ok(10).map { it + 20 }.get()
// value = 30
<V, E, F> Result<V, E>.mapError(transform: (E) -> F): Result<V, F>
Maps a Result<V, E> to Result<V, F> by applying a function to a contained Err
value, leaving an Ok
value untouched.
This function can be used to pass through a successful result while handling an error.
val error = Ok(55).andThen { Err(100) }.mapError { Err(500) }.getError()
// error = 500
<V, E> Result<V, E>.iterator(): Iterator<V>
Returns an Iterator
over the possibly contained value.
The iterator yields one value if the result is Ok
, otherwise throws NoSuchElementException
.
val iterator = Ok("hello").iterator()
// iterator.hasNext() = true
// iterator.next() = "hello"
<V, E> Result<V, E>.mutableIterator(): MutableIterator<V>
Returns a MutableIterator
over the possibly contained value.
The iterator yields one value if the result is Ok
, otherwise throws NoSuchElementException
.
val iterator = Ok("hello").mutableIterator()
iterator.remove()
// iterator.hasNext() = false
// iterator.next() throws NoSuchElementException
<V, E> Result<V, E>.and(result: () -> Result<V, E>): Result<V, E>
Returns result
if the result is Ok
, otherwise returns the Err
value of this
.
val error = Ok(300).and { Err("hello world") }.getError()
// error = "hello world"
<V, E, U> Result<V, E>.andThen(transform: (V) -> Result<U, E>): Result<U, E>
Calls transform
if the result is Ok
, otherwise returns the Err
value of this
.
This function can be used for control flow based on Result
values.
val value = Ok(20).andThen { Ok(it + 30) }.andThen { Ok(it + 40) }.get()
// value = 90
<V, E> Result<V, E>.or(result: () -> Result<V, E>): Result<V, E>
Returns result
if the result is Err
, otherwise returns the Ok
value of self
.
val value = Ok(500).or { Ok(1000) }.get()
// value = 500
<V, E> Result<V, E>.orElse(transform: (E) -> Result<V, E>): Result<V, E>
Calls transform
if the result is Err
, otherwise returns the Ok
value of this
.
This function can be used for control flow based on result values.
val value = Err(4000).orElse { Ok(2000) }.get()
// value = 2000
<V, E> Result<V, E>.getOr(default: () -> V): V
Unwraps a result, yielding the content of an Ok
. Else, it returns default
.
val value = Err("error").getOr { "default" }
// value = default
<V, E> Result<V, E>.getOrElse(transform: (E) -> V): V
Unwraps a result, yielding the content of an Ok
. If the value is an Err
then it calls transform
with its value.
val value = Err("hello").getOrElse { "world" }
// value = "world"
<V, E> Result<V, E>.unwrap(): V
Unwraps a result, yielding the content of an Ok
.
Throws an UnwrapException
if the value is an Err
, with a message provided by the Err
's value.
Err(5000).unwrap()
// throws UnwrapException("called Result.wrap on an Err value 5000")
<V, E> Result<V, E>.expect(message: () -> Any): V
Unwraps a result, yielding the content of an Ok
.
Throws an UnwrapException
if the value is an Err
, with a message including the passed message
, and the content of the Err
.
Err(1994).expect { "the year should be" }
// throws UnwrapException("the year should be 1994")
Unwraps a result, yielding the content of an Err
.
Throws an UnwrapException
if the value is an Ok
, with a custom message provided by the Ok
's value.
val error = Err("example").unwrapError()
// error = "example"
<V, E> Result<V, E>.expectError(message: () -> Any): E
Unwraps a result, yielding the content of an Err
.
Throws an UnwrapException
if the value is an Ok
, with a message including the passed message
, and the content of the Ok
.
Ok(2010).expectError { "the year should be" }
// throws UnwrapException("the year should be 2010")