-
Notifications
You must be signed in to change notification settings - Fork 77
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 = nullConverts 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, U> Result<V, E>.mapOr(default: U, transform: (V) -> U): U
Applies a function to the contained value (if Ok), or returns the provided default (if Err).
Arguments passed to mapOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use mapOrElse, which is lazily evaluated.
val result = Ok("foo").mapOr(42, String::length)
// result = 3
val result = Err("foo").mapOr(42, String::length)
// result = 42<V, E, U> Result<V, E>.mapOrElse(default: (E) -> U, transform: (V) -> U): U
Maps a Result<V, E> to U by applying a function to a contained Ok value, or a fallback function to a contained Err value.
This function can be used to unpack a successful result while handling an error.
val k = 21
val result = Ok("foo").mapOrElse({ k * 2 }, String::length)
// result = 3
val k = 21
val result = Err("foo").mapOrElse({ k * 2 }, String::length)
// result = 42<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 result = Ok(55).andThen { Err(100) }.mapError { it * 5 }
// result = Err(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 this Result<V, E> is Ok, otherwise returns the Err value of this.
val result = Ok(300).and(Err("hello world"))
// result = Err("hello world")<V, E, U> Result<V, E>.andThen(transform: (V) -> Result<U, E>): Result<U, E>
Calls transform if this Result<V, E> is Ok, otherwise returns the Err value of this.
This function can be used for control flow based on Result values.
fun sq(x: Int) = Ok(x * x)
fun err(x: Int) = Err(x)
val result = Ok(2).andThen(::sq).andThen(::sq)
// result = Ok(16)
val result = Ok(2).andThen(::sq).andThen(::err)
// result = Err(4)
val result = Ok(2).andThen(::err).andThen(::sq)
// result = Err(2)
val result = Err(3).andThen(::sq).andThen(::sq)
// result = Err(3)<V, E> Result<V, E>.or(result: Result<V, E>): Result<V, E>
Returns result if this Result<V, E> is Err, otherwise returns the Ok value of this.
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<V, E>, yielding the content of an Ok. Else, it returns default.
Arguments passed to getOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use getOrElse, which is lazily evaluated.
val value = Err("error").getOr { "default" }
// value = default<V, E> Result<V, E>.getOrElse(transform: (E) -> V): V
Returns the contained Ok value or computes it from a function.
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<V, E>, 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")