Skip to content

Commit

Permalink
Added contracts to throwIf and throwUnless
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Cooper committed Oct 26, 2021
1 parent b07a57d commit b26cd5f
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -8,8 +11,10 @@ package com.github.michaelbull.result
*/
public inline fun <V : Any?, E : Throwable> Result<V, E>.throwIf(
predicate: (E) -> Boolean
): Result<V, E> =
onFailure { e -> if (predicate(e)) throw e }
): Result<V, E> {
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]
Expand Down

0 comments on commit b26cd5f

Please sign in to comment.