Remove deprecation of eager-evaluating functions

They are fine to be used, and their lazy equivalents already have analogous
methods that can be used instead.
This commit is contained in:
Michael Bull 2020-08-26 19:15:11 +01:00
parent 4206f8b905
commit a76768fa42
7 changed files with 57 additions and 57 deletions

View File

@ -3,25 +3,25 @@ package com.github.michaelbull.result
import kotlin.contracts.InvocationKind import kotlin.contracts.InvocationKind
import kotlin.contracts.contract import kotlin.contracts.contract
@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]. * 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) * - Rust: [Result.and](https://doc.rust-lang.org/std/result/enum.Result.html#method.and)
*/ */
infix fun <V, E> Result<V, E>.and(result: Result<V, E>): Result<V, E> {
return when (this) {
is Ok -> result
is Err -> this
}
}
@Deprecated("Use andThen instead", ReplaceWith("andThen { result() }"))
inline infix fun <V, E> Result<V, E>.and(result: () -> Result<V, E>): Result<V, E> { inline infix fun <V, E> Result<V, E>.and(result: () -> Result<V, E>): Result<V, E> {
contract { contract {
callsInPlace(result, InvocationKind.AT_MOST_ONCE) callsInPlace(result, InvocationKind.AT_MOST_ONCE)
} }
return when (this) { return andThen { result() }
is Ok -> result()
is Err -> this
}
} }
/** /**

View File

@ -38,11 +38,6 @@ 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]. * Returns the [value][Ok.value] if this [Result] is [Ok], otherwise [default].
* *
@ -53,20 +48,20 @@ infix fun <V, E> Result<V, E>.getOr(default: V): V {
* @param default The value to return if [Err]. * @param default The value to return if [Err].
* @return The [value][Ok.value] if [Ok], otherwise [default]. * @return The [value][Ok.value] if [Ok], otherwise [default].
*/ */
infix fun <V, E> Result<V, E>.getOr(default: V): V {
return when (this) {
is Ok -> value
is Err -> default
}
}
@Deprecated("Use getOrElse instead", ReplaceWith("getOrElse { default() }"))
inline infix fun <V, E> Result<V, E>.getOr(default: () -> V): V { inline infix fun <V, E> Result<V, E>.getOr(default: () -> V): V {
contract { contract {
callsInPlace(default, InvocationKind.AT_MOST_ONCE) callsInPlace(default, InvocationKind.AT_MOST_ONCE)
} }
return when (this) { return getOrElse { default() }
is Ok -> value
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 }
} }
/** /**
@ -77,15 +72,20 @@ infix fun <V, E> Result<V, E>.getErrorOr(default: E): E {
* @param default The error to return if [Ok]. * @param default The error to return if [Ok].
* @return The [error][Err.error] if [Err], otherwise [default]. * @return The [error][Err.error] if [Err], otherwise [default].
*/ */
infix fun <V, E> Result<V, E>.getErrorOr(default: E): E {
return when (this) {
is Ok -> default
is Err -> error
}
}
@Deprecated("Use getOrElse instead", ReplaceWith("getErrorOrElse { default() }"))
inline infix fun <V, E> Result<V, E>.getErrorOr(default: () -> E): E { inline infix fun <V, E> Result<V, E>.getErrorOr(default: () -> E): E {
contract { contract {
callsInPlace(default, InvocationKind.AT_MOST_ONCE) callsInPlace(default, InvocationKind.AT_MOST_ONCE)
} }
return when (this) { return getErrorOrElse { default() }
is Ok -> default()
is Err -> error
}
} }
/** /**

View File

@ -3,25 +3,25 @@ package com.github.michaelbull.result
import kotlin.contracts.InvocationKind import kotlin.contracts.InvocationKind
import kotlin.contracts.contract import kotlin.contracts.contract
@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]. * 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) * - Rust: [Result.or](https://doc.rust-lang.org/std/result/enum.Result.html#method.or)
*/ */
infix fun <V, E> Result<V, E>.or(result: Result<V, E>): Result<V, E> {
return when (this) {
is Ok -> this
is Err -> result
}
}
@Deprecated("Use orElse instead", ReplaceWith("orElse { result() }"))
inline infix fun <V, E> Result<V, E>.or(result: () -> Result<V, E>): Result<V, E> { inline infix fun <V, E> Result<V, E>.or(result: () -> Result<V, E>): Result<V, E> {
contract { contract {
callsInPlace(result, InvocationKind.AT_MOST_ONCE) callsInPlace(result, InvocationKind.AT_MOST_ONCE)
} }
return when (this) { return orElse { result() }
is Ok -> this
is Err -> result()
}
} }
/** /**

View File

@ -12,7 +12,7 @@ class AndTest {
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
expected = 500, expected = 500,
actual = Ok(230).and { Ok(500) }.get() actual = Ok(230).and(Ok(500)).get()
) )
} }
@ -20,7 +20,7 @@ class AndTest {
fun returnsValueIfErr() { fun returnsValueIfErr() {
assertEquals( assertEquals(
expected = "hello world", 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 @@ class GetTest {
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
expected = "hello", expected = "hello",
actual = Ok("hello").getOr { "world" } actual = Ok("hello").getOr("world")
) )
} }
@ -48,7 +48,7 @@ class GetTest {
fun returnsDefaultValueIfErr() { fun returnsDefaultValueIfErr() {
assertEquals( assertEquals(
expected = "default", expected = "default",
actual = Err("error").getOr { "default" } actual = Err("error").getOr("default")
) )
} }
} }
@ -58,7 +58,7 @@ class GetTest {
fun returnsDefaultValueIfOk() { fun returnsDefaultValueIfOk() {
assertEquals( assertEquals(
expected = "world", expected = "world",
actual = Ok("hello").getErrorOr { "world" } actual = Ok("hello").getErrorOr("world")
) )
} }
@ -66,7 +66,7 @@ class GetTest {
fun returnsErrorIfErr() { fun returnsErrorIfErr() {
assertEquals( assertEquals(
expected = "hello", expected = "hello",
actual = Err("hello").getErrorOr { "world" } actual = Err("hello").getErrorOr("world")
) )
} }
} }

View File

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

View File

@ -31,7 +31,7 @@ class ZipTest {
fun returnsErrIfOneOfTwoErr() { fun returnsErrIfOneOfTwoErr() {
val result = zip( val result = zip(
{ Ok(10) }, { Ok(10) },
{ Ok(20).and { Err("hello") } }, { Ok(20).and(Err("hello")) },
Int::plus Int::plus
) )
@ -46,8 +46,8 @@ class ZipTest {
@Test @Test
fun returnsFirstErrIfBothErr() { fun returnsFirstErrIfBothErr() {
val result = zip( val result = zip(
{ Ok(10).and { Err("foo") } }, { Ok(10).and(Err("foo")) },
{ Ok(20).and { Err("bar") } }, { Ok(20).and(Err("bar")) },
Int::plus Int::plus
) )
@ -80,7 +80,7 @@ class ZipTest {
fun returnsErrIfOneOfThreeErr() { fun returnsErrIfOneOfThreeErr() {
val result = zip( val result = zip(
{ Ok("foo") }, { Ok("foo") },
{ Ok(1).and { Err("bar") } }, { Ok(1).and(Err("bar")) },
{ Ok(false) }, { Ok(false) },
::ZipData3 ::ZipData3
) )
@ -97,8 +97,8 @@ class ZipTest {
fun returnsFirstErrIfTwoOfThreeErr() { fun returnsFirstErrIfTwoOfThreeErr() {
val result = zip( val result = zip(
{ Ok("foo") }, { Ok("foo") },
{ Ok(1).and { Err("bar") } }, { Ok(1).and(Err("bar")) },
{ Ok(false).and { Err("baz") } }, { Ok(false).and(Err("baz")) },
::ZipData3 ::ZipData3
) )
@ -111,9 +111,9 @@ class ZipTest {
@Test @Test
fun returnsFirstErrIfAllThreeErr() { fun returnsFirstErrIfAllThreeErr() {
val result = zip( val result = zip(
{ Ok("foo").and { Err(1) } }, { Ok("foo").and(Err(1)) },
{ Ok(1).and { Err(2) } }, { Ok(1).and(Err(2)) },
{ Ok(false).and { Err(3) } }, { Ok(false).and(Err(3)) },
::ZipData3 ::ZipData3
) )
@ -147,9 +147,9 @@ class ZipTest {
fun returnsFirstErrIfSomeOfFourErr() { fun returnsFirstErrIfSomeOfFourErr() {
val result = zip( val result = zip(
{ Ok("hello") }, { Ok("hello") },
{ Ok(2).and { Err(1) } }, { Ok(2).and(Err(1)) },
{ Ok(false) }, { Ok(false) },
{ Ok(1.5).and { Err(2) } }, { Ok(1.5).and(Err(2)) },
::ZipData4 ::ZipData4
) )
@ -183,11 +183,11 @@ class ZipTest {
@Test @Test
fun returnsFirstErrIfSomeOfFiveErr() { fun returnsFirstErrIfSomeOfFiveErr() {
val result = zip( val result = zip(
{ Ok("hello").and { Err(1) } }, { Ok("hello").and(Err(1)) },
{ Ok(2) }, { Ok(2) },
{ Ok(false) }, { Ok(false) },
{ Ok(1.5) }, { Ok(1.5) },
{ Ok('a').and { Err(2) } }, { Ok('a').and(Err(2)) },
::ZipData5 ::ZipData5
) )