From c6be93142d157acb1db155a393782ed912613a73 Mon Sep 17 00:00:00 2001 From: Michael Bull Date: Tue, 9 Jan 2018 20:33:00 +0000 Subject: [PATCH] Add lazy variants for {and,or,getOr,getErrorOr} --- .../com/github/michaelbull/result/And.kt | 9 +++++++-- .../com/github/michaelbull/result/Get.kt | 18 ++++++++++++++---- .../kotlin/com/github/michaelbull/result/Or.kt | 9 +++++++-- .../com/github/michaelbull/result/Unwrap.kt | 4 ++-- .../com/github/michaelbull/result/AndTest.kt | 4 ++-- .../com/github/michaelbull/result/GetTest.kt | 8 ++++---- .../com/github/michaelbull/result/OrTest.kt | 4 ++-- 7 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/com/github/michaelbull/result/And.kt b/src/main/kotlin/com/github/michaelbull/result/And.kt index 2fe5dfc..fcffb58 100644 --- a/src/main/kotlin/com/github/michaelbull/result/And.kt +++ b/src/main/kotlin/com/github/michaelbull/result/And.kt @@ -1,5 +1,10 @@ package com.github.michaelbull.result +@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("and { result }")) +infix fun Result.and(result: Result): Result { + 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 Result.and(result: Result): Result { +infix inline fun Result.and(result: () -> Result): Result { return when (this) { - is Ok -> result + is Ok -> result() is Err -> this } } diff --git a/src/main/kotlin/com/github/michaelbull/result/Get.kt b/src/main/kotlin/com/github/michaelbull/result/Get.kt index ec76c96..197fffb 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Get.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Get.kt @@ -29,6 +29,11 @@ fun Result.getError(): E? { } } +@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("getOr { default }")) +infix fun Result.getOr(default: V): V { + return getOr { default } +} + /** * Returns the [value][Ok.value] if this [Result] is [Ok], otherwise [default]. * @@ -39,13 +44,18 @@ fun Result.getError(): E? { * @param default The value to return if [Err]. * @return The [value][Ok.value] if [Ok], otherwise [default]. */ -infix fun Result.getOr(default: V): V { +infix inline fun Result.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 Result.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 Result.getOr(default: V): V { * @param default The error to return if [Ok]. * @return The [error][Err.error] if [Err], otherwise [default]. */ -infix fun Result.getErrorOr(default: E): E { +infix inline fun Result.getErrorOr(default: () -> E): E { return when (this) { - is Ok -> default + is Ok -> default() is Err -> error } } diff --git a/src/main/kotlin/com/github/michaelbull/result/Or.kt b/src/main/kotlin/com/github/michaelbull/result/Or.kt index 0661139..e1f6c56 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Or.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Or.kt @@ -1,5 +1,10 @@ package com.github.michaelbull.result +@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("or { result }")) +infix fun Result.or(result: Result): Result { + 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 Result.or(result: Result): Result { +infix inline fun Result.or(result: () -> Result): Result { return when (this) { is Ok -> this - is Err -> result + is Err -> result() } } diff --git a/src/main/kotlin/com/github/michaelbull/result/Unwrap.kt b/src/main/kotlin/com/github/michaelbull/result/Unwrap.kt index ec1b87f..2e94ac1 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Unwrap.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Unwrap.kt @@ -16,7 +16,7 @@ fun Result.unwrap(): V { } } -@Deprecated("Use lazy evaluation.", ReplaceWith("expect { message }")) +@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("expect { message }")) infix fun Result.expect(message: String): V { return expect { message } } @@ -50,7 +50,7 @@ fun Result.unwrapError(): E { } } -@Deprecated("Use lazy evaluation.", ReplaceWith("expectError { message }")) +@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("expectError { message }")) infix fun Result.expectError(message: String): E { return expectError { message } } diff --git a/src/test/kotlin/com/github/michaelbull/result/AndTest.kt b/src/test/kotlin/com/github/michaelbull/result/AndTest.kt index 1ecd845..6362a74 100644 --- a/src/test/kotlin/com/github/michaelbull/result/AndTest.kt +++ b/src/test/kotlin/com/github/michaelbull/result/AndTest.kt @@ -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() ) } } diff --git a/src/test/kotlin/com/github/michaelbull/result/GetTest.kt b/src/test/kotlin/com/github/michaelbull/result/GetTest.kt index d8e6a46..876b264 100644 --- a/src/test/kotlin/com/github/michaelbull/result/GetTest.kt +++ b/src/test/kotlin/com/github/michaelbull/result/GetTest.kt @@ -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" } ) } } diff --git a/src/test/kotlin/com/github/michaelbull/result/OrTest.kt b/src/test/kotlin/com/github/michaelbull/result/OrTest.kt index c0ad1b1..df17c97 100644 --- a/src/test/kotlin/com/github/michaelbull/result/OrTest.kt +++ b/src/test/kotlin/com/github/michaelbull/result/OrTest.kt @@ -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() ) } }