Add compiler contract to Result#toErrorIf

This commit is contained in:
Michael Bull 2020-04-18 11:43:31 +01:00
parent 2ef738abcb
commit 22898fff4f

View File

@ -172,6 +172,11 @@ inline infix fun <V, E, U> Result<V, E>.flatMap(transform: (V) -> Result<U, E>):
* and satisfies the given [predicate], otherwise this [Result].
*/
inline fun <V, E> Result<V, E>.toErrorIf(predicate: (V) -> Boolean, transform: (V) -> E): Result<V, E> {
contract {
callsInPlace(predicate, InvocationKind.AT_MOST_ONCE)
callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
}
return when (this) {
is Ok -> if (predicate(value)) {
Err(transform(value))