diff --git a/src/main/kotlin/com/github/michaelbull/result/Unwrap.kt b/src/main/kotlin/com/github/michaelbull/result/Unwrap.kt index 005dd09..1513a74 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Unwrap.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Unwrap.kt @@ -16,6 +16,11 @@ fun Result.unwrap(): V { } } +@Deprecated("Use lazy evaluation.", ReplaceWith("expect { message }")) +infix fun Result.expect(message: String): V { + return expect { message } +} + /** * Unwraps a [Result], yielding the [value][Ok.value]. * @@ -24,10 +29,10 @@ fun Result.unwrap(): V { * @param message The message to include in the [UnwrapException] if the [Result] is an [Err]. * @throws UnwrapException if the [Result] is an [Err], with the specified [message]. */ -infix fun Result.expect(message: String): V { +infix inline fun Result.expect(message: () -> String): V { return when (this) { is Ok -> value - is Err -> throw UnwrapException("$message $error") + is Err -> throw UnwrapException("${message()} $error") } } @@ -45,6 +50,11 @@ fun Result.unwrapError(): E { } } +@Deprecated("Use lazy evaluation.", ReplaceWith("expectError { message }")) +infix fun Result.expectError(message: String): E { + return expectError { message } +} + /** * Unwraps a [Result], yielding the [error][Err.error]. * @@ -53,9 +63,9 @@ fun Result.unwrapError(): E { * @param message The message to include in the [UnwrapException] if the [Result] is [Ok]. * @throws UnwrapException if the [Result] is [Ok], with the specified [message]. */ -infix fun Result.expectError(message: String): E { +infix inline fun Result.expectError(message: () -> String): E { return when (this) { - is Ok -> throw UnwrapException("$message $value") + is Ok -> throw UnwrapException("${message()} $value") is Err -> error } } diff --git a/src/test/kotlin/com/github/michaelbull/result/UnwrapTest.kt b/src/test/kotlin/com/github/michaelbull/result/UnwrapTest.kt index 044bc99..584e69f 100644 --- a/src/test/kotlin/com/github/michaelbull/result/UnwrapTest.kt +++ b/src/test/kotlin/com/github/michaelbull/result/UnwrapTest.kt @@ -23,14 +23,14 @@ internal class UnwrapTest { @Test internal fun `expect should return the result value if ok`() { - val value = Ok(1994).expect("the year should be") + val value = Ok(1994).expect { "the year should be" } assertThat(value, equalTo(1994)) } @Test internal fun `expect should throw an UnwrapException with a specified message if not ok`() { val throwable = assertThrows(UnwrapException::class.java, { - Err(1994).expect("the year should be") + Err(1994).expect { "the year should be" } }) assertThat(throwable.message, equalTo("the year should be 1994")) @@ -54,7 +54,7 @@ internal class UnwrapTest { @Test internal fun `expectError should throw an UnwrapException with a specified message if ok`() { val throwable = assertThrows(UnwrapException::class.java, { - Ok(2010).expectError("the year should be") + Ok(2010).expectError { "the year should be" } }) assertThat(throwable.message, equalTo("the year should be 2010")) @@ -62,7 +62,7 @@ internal class UnwrapTest { @Test internal fun `expectError should return the result error if not ok`() { - val error = Err(2010).expectError("the year should be") + val error = Err(2010).expectError { "the year should be" } assertThat(error, equalTo(2010)) } }