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.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].
*
* - 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> {
contract {
callsInPlace(result, InvocationKind.AT_MOST_ONCE)
}
return when (this) {
is Ok -> result()
is Err -> this
}
return andThen { result() }
}
/**

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].
*
@ -53,20 +48,20 @@ infix fun <V, E> Result<V, E>.getOr(default: V): V {
* @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 {
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 {
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 <V, E> Result<V, E>.getErrorOr(default: E): E {
return getErrorOr { default }
return getOrElse { default() }
}
/**
@ -77,15 +72,20 @@ infix fun <V, E> Result<V, E>.getErrorOr(default: E): E {
* @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 {
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 {
contract {
callsInPlace(default, InvocationKind.AT_MOST_ONCE)
}
return when (this) {
is Ok -> default()
is Err -> error
}
return getErrorOrElse { default() }
}
/**

View File

@ -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 <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].
*
* - 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> {
contract {
callsInPlace(result, InvocationKind.AT_MOST_ONCE)
}
return when (this) {
is Ok -> this
is Err -> result()
}
return orElse { result() }
}
/**

View File

@ -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()
)
}
}

View File

@ -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")
)
}
}

View File

@ -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()
)
}
}

View File

@ -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
)