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

View File

@ -1,5 +1,8 @@
package com.github.michaelbull.result 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]. * 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 * @sample com.github.michaelbull.result.bind.ResultBindingTest
*/ */
inline fun <V, E> binding(crossinline block: ResultBinding<E>.() -> V): Result<V, E> { inline fun <V, E> binding(crossinline block: ResultBinding<E>.() -> V): Result<V, E> {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
val receiver = ResultBindingImpl<E>() val receiver = ResultBindingImpl<E>()
return try { return try {