diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/And.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/And.kt index 0022847..68cb6a7 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/And.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/And.kt @@ -3,25 +3,25 @@ package com.github.michaelbull.result import kotlin.contracts.InvocationKind import kotlin.contracts.contract -@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]. * * - Rust: [Result.and](https://doc.rust-lang.org/std/result/enum.Result.html#method.and) */ +infix fun Result.and(result: Result): Result { + return when (this) { + is Ok -> result + is Err -> this + } +} + +@Deprecated("Use andThen instead", ReplaceWith("andThen { result() }")) inline infix fun Result.and(result: () -> Result): Result { contract { callsInPlace(result, InvocationKind.AT_MOST_ONCE) } - return when (this) { - is Ok -> result() - is Err -> this - } + return andThen { result() } } /** diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Get.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Get.kt index 7034f5d..32035b0 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Get.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Get.kt @@ -38,11 +38,6 @@ 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]. * @@ -53,20 +48,20 @@ infix fun Result.getOr(default: V): V { * @param default The value to return if [Err]. * @return The [value][Ok.value] if [Ok], otherwise [default]. */ +infix fun Result.getOr(default: V): V { + return when (this) { + is Ok -> value + is Err -> default + } +} + +@Deprecated("Use getOrElse instead", ReplaceWith("getOrElse { default() }")) inline infix fun Result.getOr(default: () -> V): V { contract { callsInPlace(default, InvocationKind.AT_MOST_ONCE) } - return when (this) { - is Ok -> value - is Err -> default() - } -} - -@Deprecated("Use lazy-evaluating variant instead", ReplaceWith("getErrorOr { default }")) -infix fun Result.getErrorOr(default: E): E { - return getErrorOr { default } + return getOrElse { default() } } /** @@ -77,15 +72,20 @@ infix fun Result.getErrorOr(default: E): E { * @param default The error to return if [Ok]. * @return The [error][Err.error] if [Err], otherwise [default]. */ +infix fun Result.getErrorOr(default: E): E { + return when (this) { + is Ok -> default + is Err -> error + } +} + +@Deprecated("Use getOrElse instead", ReplaceWith("getErrorOrElse { default() }")) inline infix fun Result.getErrorOr(default: () -> E): E { contract { callsInPlace(default, InvocationKind.AT_MOST_ONCE) } - return when (this) { - is Ok -> default() - is Err -> error - } + return getErrorOrElse { default() } } /** diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Or.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Or.kt index fe3e22c..b74d540 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Or.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Or.kt @@ -3,25 +3,25 @@ package com.github.michaelbull.result import kotlin.contracts.InvocationKind import kotlin.contracts.contract -@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]. * * - Rust: [Result.or](https://doc.rust-lang.org/std/result/enum.Result.html#method.or) */ +infix fun Result.or(result: Result): Result { + return when (this) { + is Ok -> this + is Err -> result + } +} + +@Deprecated("Use orElse instead", ReplaceWith("orElse { result() }")) inline infix fun Result.or(result: () -> Result): Result { contract { callsInPlace(result, InvocationKind.AT_MOST_ONCE) } - return when (this) { - is Ok -> this - is Err -> result() - } + return orElse { result() } } /** diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/AndTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/AndTest.kt index ee4d39b..4f80d54 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/AndTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/AndTest.kt @@ -12,7 +12,7 @@ class AndTest { fun returnsValueIfOk() { assertEquals( expected = 500, - actual = Ok(230).and { Ok(500) }.get() + actual = Ok(230).and(Ok(500)).get() ) } @@ -20,7 +20,7 @@ class AndTest { 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/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/GetTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/GetTest.kt index 510e2b9..5e492a7 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/GetTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/GetTest.kt @@ -40,7 +40,7 @@ class GetTest { fun returnsValueIfOk() { assertEquals( expected = "hello", - actual = Ok("hello").getOr { "world" } + actual = Ok("hello").getOr("world") ) } @@ -48,7 +48,7 @@ class GetTest { fun returnsDefaultValueIfErr() { assertEquals( expected = "default", - actual = Err("error").getOr { "default" } + actual = Err("error").getOr("default") ) } } @@ -58,7 +58,7 @@ class GetTest { fun returnsDefaultValueIfOk() { assertEquals( expected = "world", - actual = Ok("hello").getErrorOr { "world" } + actual = Ok("hello").getErrorOr("world") ) } @@ -66,7 +66,7 @@ class GetTest { fun returnsErrorIfErr() { assertEquals( expected = "hello", - actual = Err("hello").getErrorOr { "world" } + actual = Err("hello").getErrorOr("world") ) } } diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OrTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OrTest.kt index 68c217f..a47a700 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OrTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OrTest.kt @@ -11,7 +11,7 @@ class OrTest { fun returnsValueIfOk() { assertEquals( expected = 500, - actual = Ok(500).or { Ok(1000) }.get() + actual = Ok(500).or(Ok(1000)).get() ) } @@ -19,7 +19,7 @@ class OrTest { fun returnsDefaultValueIfErr() { assertEquals( expected = 5000, - actual = Err(OrError).or { Ok(5000) }.get() + actual = Err(OrError).or(Ok(5000)).get() ) } } diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/ZipTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/ZipTest.kt index 63832a0..56374cf 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/ZipTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/ZipTest.kt @@ -31,7 +31,7 @@ class ZipTest { fun returnsErrIfOneOfTwoErr() { val result = zip( { Ok(10) }, - { Ok(20).and { Err("hello") } }, + { Ok(20).and(Err("hello")) }, Int::plus ) @@ -46,8 +46,8 @@ class ZipTest { @Test fun returnsFirstErrIfBothErr() { val result = zip( - { Ok(10).and { Err("foo") } }, - { Ok(20).and { Err("bar") } }, + { Ok(10).and(Err("foo")) }, + { Ok(20).and(Err("bar")) }, Int::plus ) @@ -80,7 +80,7 @@ class ZipTest { fun returnsErrIfOneOfThreeErr() { val result = zip( { Ok("foo") }, - { Ok(1).and { Err("bar") } }, + { Ok(1).and(Err("bar")) }, { Ok(false) }, ::ZipData3 ) @@ -97,8 +97,8 @@ class ZipTest { fun returnsFirstErrIfTwoOfThreeErr() { val result = zip( { Ok("foo") }, - { Ok(1).and { Err("bar") } }, - { Ok(false).and { Err("baz") } }, + { Ok(1).and(Err("bar")) }, + { Ok(false).and(Err("baz")) }, ::ZipData3 ) @@ -111,9 +111,9 @@ class ZipTest { @Test fun returnsFirstErrIfAllThreeErr() { val result = zip( - { Ok("foo").and { Err(1) } }, - { Ok(1).and { Err(2) } }, - { Ok(false).and { Err(3) } }, + { Ok("foo").and(Err(1)) }, + { Ok(1).and(Err(2)) }, + { Ok(false).and(Err(3)) }, ::ZipData3 ) @@ -147,9 +147,9 @@ class ZipTest { fun returnsFirstErrIfSomeOfFourErr() { val result = zip( { Ok("hello") }, - { Ok(2).and { Err(1) } }, + { Ok(2).and(Err(1)) }, { Ok(false) }, - { Ok(1.5).and { Err(2) } }, + { Ok(1.5).and(Err(2)) }, ::ZipData4 ) @@ -183,11 +183,11 @@ class ZipTest { @Test fun returnsFirstErrIfSomeOfFiveErr() { val result = zip( - { Ok("hello").and { Err(1) } }, + { Ok("hello").and(Err(1)) }, { Ok(2) }, { Ok(false) }, { Ok(1.5) }, - { Ok('a').and { Err(2) } }, + { Ok('a').and(Err(2)) }, ::ZipData5 )