diff --git a/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.kt b/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.kt index eab3158..e366d9f 100644 --- a/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.kt +++ b/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.kt @@ -20,16 +20,16 @@ import kotlin.coroutines.CoroutineContext * This new scope is [cancelled][CoroutineScope.cancel] once a failing bind is encountered, eagerly cancelling all * child [jobs][Job]. */ -public suspend inline fun binding(crossinline block: suspend SuspendableResultBinding.() -> V): Result { +public suspend inline fun binding(crossinline block: suspend CoroutineBindingScope.() -> V): Result { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - lateinit var receiver: SuspendableResultBindingImpl + lateinit var receiver: CoroutineBindingScopeImpl return try { coroutineScope { - receiver = SuspendableResultBindingImpl(this) + receiver = CoroutineBindingScopeImpl(this) with(receiver) { Ok(block()) @@ -42,14 +42,20 @@ public suspend inline fun binding(crossinline block: suspend SuspendableR internal object BindCancellationException : CancellationException(null as String?) -public interface SuspendableResultBinding : CoroutineScope { +@Deprecated( + message = "Use CoroutineBindingScope instead", + replaceWith = ReplaceWith("CoroutineBindingScope") +) +public typealias SuspendableResultBinding = CoroutineBindingScope + +public interface CoroutineBindingScope : CoroutineScope { public suspend fun Result.bind(): V } @PublishedApi -internal class SuspendableResultBindingImpl( +internal class CoroutineBindingScopeImpl( delegate: CoroutineScope, -) : SuspendableResultBinding, CoroutineScope by delegate { +) : CoroutineBindingScope, CoroutineScope by delegate { private val mutex = Mutex() var result: Result? = null diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt index eac1db7..d723656 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt @@ -4,10 +4,10 @@ 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 [BindingScope] as its receiver and returns its [Result]. * - * When inside a [binding] block, the [bind][ResultBinding.bind] function is accessible on any [Result]. Calling the - * [bind][ResultBinding.bind] function will attempt to unwrap the [Result] and locally return its [value][Ok.value]. If + * When inside a [binding] block, the [bind][BindingScope.bind] function is accessible on any [Result]. Calling the + * [bind][BindingScope.bind] function will attempt to unwrap the [Result] and locally return its [value][Ok.value]. If * the [Result] is an [Err], the binding block will terminate with that bind and return that failed-to-bind [Err]. * * Example: @@ -22,12 +22,12 @@ import kotlin.contracts.contract * } * ``` */ -public inline fun binding(crossinline block: ResultBinding.() -> V): Result { +public inline fun binding(crossinline block: BindingScope.() -> V): Result { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - return with(ResultBindingImpl()) { + return with(BindingScopeImpl()) { try { Ok(block()) } catch (ex: BindException) { @@ -38,12 +38,18 @@ public inline fun binding(crossinline block: ResultBinding.() -> V): R internal expect object BindException : Exception -public interface ResultBinding { +@Deprecated( + message = "Use BindingScope instead", + replaceWith = ReplaceWith("BindingScope") +) +public typealias ResultBinding = BindingScope + +public interface BindingScope { public fun Result.bind(): V } @PublishedApi -internal class ResultBindingImpl : ResultBinding { +internal class BindingScopeImpl : BindingScope { var result: Result? = null @@ -51,7 +57,7 @@ internal class ResultBindingImpl : ResultBinding { return when (this) { is Ok -> value is Err -> { - this@ResultBindingImpl.result = this + this@BindingScopeImpl.result = this throw BindException } } diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/SuspendableBinding.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/SuspendableBinding.kt index c268620..93a9c02 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/SuspendableBinding.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/SuspendableBinding.kt @@ -1,10 +1,10 @@ package com.github.michaelbull.result.coroutines import com.github.michaelbull.result.BindException +import com.github.michaelbull.result.BindingScope +import com.github.michaelbull.result.BindingScopeImpl import com.github.michaelbull.result.Ok import com.github.michaelbull.result.Result -import com.github.michaelbull.result.ResultBinding -import com.github.michaelbull.result.ResultBindingImpl import kotlin.contracts.InvocationKind import kotlin.contracts.contract @@ -17,12 +17,12 @@ import kotlin.contracts.contract "Please import the kotlin-result-coroutines library to continue using this feature.", level = DeprecationLevel.WARNING ) -public suspend inline fun binding(crossinline block: suspend ResultBinding.() -> V): Result { +public suspend inline fun binding(crossinline block: suspend BindingScope.() -> V): Result { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - return with(ResultBindingImpl()) { + return with(BindingScopeImpl()) { try { Ok(block()) } catch (ex: BindException) {