From 22898fff4f6c0b08e749cce3ba5f52ac94f9877b Mon Sep 17 00:00:00 2001 From: Michael Bull Date: Sat, 18 Apr 2020 11:43:31 +0100 Subject: [PATCH] Add compiler contract to Result#toErrorIf --- src/main/kotlin/com/github/michaelbull/result/Map.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/kotlin/com/github/michaelbull/result/Map.kt b/src/main/kotlin/com/github/michaelbull/result/Map.kt index 61c4819..fcf419e 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Map.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Map.kt @@ -172,6 +172,11 @@ inline infix fun Result.flatMap(transform: (V) -> Result): * and satisfies the given [predicate], otherwise this [Result]. */ inline fun Result.toErrorIf(predicate: (V) -> Boolean, transform: (V) -> E): Result { + contract { + callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) + callsInPlace(transform, InvocationKind.AT_MOST_ONCE) + } + return when (this) { is Ok -> if (predicate(value)) { Err(transform(value))