-
Notifications
You must be signed in to change notification settings - Fork 78
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 Error.
val result = err(500)
if (result is Error) {
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 Error 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 Error 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 Error 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 Error 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 Error, 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 Error, 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 Error 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 Error, with a message provided by the Error's value.
err(5000).unwrap()
// throws UnwrapException("called Result.wrap on an Error value 5000")<V, E> Result<V, E>.expect(message: String): V
Unwraps a result, yielding the content of an Ok.
Throws an UnwrapException if the value is an Error, with a message including the passed message, and the content of the Error.
err(1994).expect("the year should be")
// throws UnwrapException("the year should be 1994")Unwraps a result, yielding the content of an Error.
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: String): E
Unwraps a result, yielding the content of an Error.
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")