Skip to content
Michael Bull edited this page Oct 22, 2017 · 24 revisions

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 = Error(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 = Error("example").get()
// value = 230
// error = null

Converts from Result<V, E> to E?.

val value = Ok(230).getError()
val error = Error("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 { Error(100) }.mapError { Error(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(Error("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 = Error(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 = Error("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 = Error("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.

Error(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.

Error(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 = Error("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")
Clone this wiki locally