Skip to content

Commit fec8c03

Browse files
committed
Use delegation for SuspendableResultBindingImpl
The Kotlin docs for CoroutineScope[1] explicitly state that: "Manual implementation of this interface is not recommended, implementation by delegation should be preferred instead." [1] https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/
1 parent 8d6b0a9 commit fec8c03

File tree

1 file changed

+8
-5
lines changed
  • kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding

1 file changed

+8
-5
lines changed

kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ public suspend inline fun <V, E> binding(crossinline block: suspend SuspendableR
2929

3030
return try {
3131
coroutineScope {
32-
receiver = SuspendableResultBindingImpl(this.coroutineContext)
33-
with(receiver) { Ok(block()) }
32+
receiver = SuspendableResultBindingImpl(this)
33+
34+
with(receiver) {
35+
Ok(block())
36+
}
3437
}
3538
} catch (ex: BindCancellationException) {
3639
receiver.result
@@ -45,8 +48,8 @@ public interface SuspendableResultBinding<E> : CoroutineScope {
4548

4649
@PublishedApi
4750
internal class SuspendableResultBindingImpl<E>(
48-
override val coroutineContext: CoroutineContext,
49-
) : SuspendableResultBinding<E> {
51+
delegate: CoroutineScope,
52+
) : SuspendableResultBinding<E>, CoroutineScope by delegate {
5053

5154
private val mutex = Mutex()
5255
lateinit var result: Err<E>
@@ -57,7 +60,7 @@ internal class SuspendableResultBindingImpl<E>(
5760
is Err -> mutex.withLock {
5861
if (::result.isInitialized.not()) {
5962
result = this
60-
this@SuspendableResultBindingImpl.cancel(BindCancellationException)
63+
coroutineContext.cancel(BindCancellationException)
6164
}
6265

6366
throw BindCancellationException

0 commit comments

Comments
 (0)