Add compiler contracts to binding function

This commit is contained in:
Michael Bull 2020-05-11 12:48:41 +01:00
parent 534815400c
commit ff8753321d
1 changed files with 7 additions and 0 deletions

View File

@ -1,5 +1,8 @@
package com.github.michaelbull.result
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
/**
* Calls the specified function [block] with [ResultBinding] as its receiver and returns its [Result].
*
@ -22,6 +25,10 @@ package com.github.michaelbull.result
* @sample com.github.michaelbull.result.bind.ResultBindingTest
*/
inline fun <V, E> binding(crossinline block: ResultBinding<E>.() -> V): Result<V, E> {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
val receiver = ResultBindingImpl<E>()
return try {