From b26cd5fcdd11343213b2aa01b9dcfa72dea768e5 Mon Sep 17 00:00:00 2001 From: Joseph Cooper Date: Tue, 26 Oct 2021 15:33:08 +0100 Subject: [PATCH] Added contracts to throwIf and throwUnless --- .../com/github/michaelbull/result/ThrowIfUnless.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/ThrowIfUnless.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/ThrowIfUnless.kt index ad93c40..087e6ad 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/ThrowIfUnless.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/ThrowIfUnless.kt @@ -1,5 +1,8 @@ package com.github.michaelbull.result +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract + /** * If the [Result] is [Err] and contains a [Throwable], throws the [error][Err.error] * if the [predicate] returns true. @@ -8,8 +11,10 @@ package com.github.michaelbull.result */ public inline fun Result.throwIf( predicate: (E) -> Boolean -): Result = - onFailure { e -> if (predicate(e)) throw e } +): Result { + contract { callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) } + return onFailure { e -> if (predicate(e)) throw e } +} /** * If the [Result] is [Err] and contains a [Throwable], throws the [error][Err.error]