Add lazy variants for {and,or,getOr,getErrorOr}

This commit is contained in:
Michael Bull 2018-01-09 20:33:00 +00:00
parent 17f46f3826
commit c6be93142d
7 changed files with 38 additions and 18 deletions

View File

@ -1,5 +1,10 @@
package com.github.michaelbull.result
@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("and { result }"))
infix fun <V, E> Result<V, E>.and(result: Result<V, E>): Result<V, E> {
return and { result }
}
/**
* Returns [result] if this [Result] is [Ok], otherwise this [Err].
*
@ -8,9 +13,9 @@ package com.github.michaelbull.result
* @param result The [Result] to return if [Ok].
* @return The [result] if [Ok], otherwise [Err].
*/
infix fun <V, E> Result<V, E>.and(result: Result<V, E>): Result<V, E> {
infix inline fun <V, E> Result<V, E>.and(result: () -> Result<V, E>): Result<V, E> {
return when (this) {
is Ok -> result
is Ok -> result()
is Err -> this
}
}

View File

@ -29,6 +29,11 @@ fun <V, E> Result<V, E>.getError(): E? {
}
}
@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("getOr { default }"))
infix fun <V, E> Result<V, E>.getOr(default: V): V {
return getOr { default }
}
/**
* Returns the [value][Ok.value] if this [Result] is [Ok], otherwise [default].
*
@ -39,13 +44,18 @@ fun <V, E> Result<V, E>.getError(): E? {
* @param default The value to return if [Err].
* @return The [value][Ok.value] if [Ok], otherwise [default].
*/
infix fun <V, E> Result<V, E>.getOr(default: V): V {
infix inline fun <V, E> Result<V, E>.getOr(default: () -> V): V {
return when (this) {
is Ok -> value
is Err -> default
is Err -> default()
}
}
@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("getErrorOr { default }"))
infix fun <V, E> Result<V, E>.getErrorOr(default: E): E {
return getErrorOr { default }
}
/**
* Returns the [error][Err.error] if this [Result] is [Err], otherwise [default].
*
@ -54,9 +64,9 @@ infix fun <V, E> Result<V, E>.getOr(default: V): V {
* @param default The error to return if [Ok].
* @return The [error][Err.error] if [Err], otherwise [default].
*/
infix fun <V, E> Result<V, E>.getErrorOr(default: E): E {
infix inline fun <V, E> Result<V, E>.getErrorOr(default: () -> E): E {
return when (this) {
is Ok -> default
is Ok -> default()
is Err -> error
}
}

View File

@ -1,5 +1,10 @@
package com.github.michaelbull.result
@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("or { result }"))
infix fun <V, E> Result<V, E>.or(result: Result<V, E>): Result<V, E> {
return or { result }
}
/**
* Returns [result] if this [Result] is [Err], otherwise this [Ok].
*
@ -8,10 +13,10 @@ package com.github.michaelbull.result
* @param result The [Result] to return if [Err].
* @return The [result] if [Err], otherwise [Ok].
*/
infix fun <V, E> Result<V, E>.or(result: Result<V, E>): Result<V, E> {
infix inline fun <V, E> Result<V, E>.or(result: () -> Result<V, E>): Result<V, E> {
return when (this) {
is Ok -> this
is Err -> result
is Err -> result()
}
}

View File

@ -16,7 +16,7 @@ fun <V, E> Result<V, E>.unwrap(): V {
}
}
@Deprecated("Use lazy evaluation.", ReplaceWith("expect { message }"))
@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("expect { message }"))
infix fun <V, E> Result<V, E>.expect(message: String): V {
return expect { message }
}
@ -50,7 +50,7 @@ fun <V, E> Result<V, E>.unwrapError(): E {
}
}
@Deprecated("Use lazy evaluation.", ReplaceWith("expectError { message }"))
@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("expectError { message }"))
infix fun <V, E> Result<V, E>.expectError(message: String): E {
return expectError { message }
}

View File

@ -12,7 +12,7 @@ internal class AndTest {
internal fun returnsValueIfOk() {
assertEquals(
expected = 500,
actual = Ok(230).and(Ok(500)).get()
actual = Ok(230).and { Ok(500) }.get()
)
}
@ -20,7 +20,7 @@ internal class AndTest {
internal fun returnsValueIfErr() {
assertEquals(
expected = "hello world",
actual = Ok(300).and(Err("hello world")).getError()
actual = Ok(300).and { Err("hello world") }.getError()
)
}
}

View File

@ -40,7 +40,7 @@ internal class GetTest {
internal fun returnsValueIfOk() {
assertEquals(
expected = "hello",
actual = Ok("hello").getOr("world")
actual = Ok("hello").getOr { "world" }
)
}
@ -48,7 +48,7 @@ internal class GetTest {
internal fun returnsDefaultValueIfErr() {
assertEquals(
expected = "default",
actual = Err("error").getOr("default")
actual = Err("error").getOr { "default" }
)
}
}
@ -58,7 +58,7 @@ internal class GetTest {
internal fun returnsDefaultValueIfOk() {
assertEquals(
expected = "world",
actual = Ok("hello").getErrorOr("world")
actual = Ok("hello").getErrorOr { "world" }
)
}
@ -66,7 +66,7 @@ internal class GetTest {
internal fun returnsErrorIfErr() {
assertEquals(
expected = "hello",
actual = Err("hello").getErrorOr("world")
actual = Err("hello").getErrorOr { "world" }
)
}
}

View File

@ -11,7 +11,7 @@ internal class OrTest {
internal fun returnsValueIfOk() {
assertEquals(
expected = 500,
actual = Ok(500).or(Ok(1000)).get()
actual = Ok(500).or { Ok(1000) }.get()
)
}
@ -19,7 +19,7 @@ internal class OrTest {
internal fun returnsDefaultValueIfErr() {
assertEquals(
expected = 5000,
actual = Err(OrError).or(Ok(5000)).get()
actual = Err(OrError).or { Ok(5000) }.get()
)
}
}