From 0b21b7361f87b3581f1f7933370060551b0d9aff Mon Sep 17 00:00:00 2001 From: Michael Bull Date: Sun, 10 Mar 2024 23:15:53 +0000 Subject: [PATCH] Replace lateinit with null in binding variants --- .../coroutines/binding/SuspendableBinding.kt | 6 +++--- .../com/github/michaelbull/result/Binding.kt | 14 +++++++------- .../result/coroutines/SuspendableBinding.kt | 12 ++++++------ 3 files changed, 16 insertions(+), 16 deletions(-) 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 88c0a9e..eab3158 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 @@ -36,7 +36,7 @@ public suspend inline fun binding(crossinline block: suspend SuspendableR } } } catch (ex: BindCancellationException) { - receiver.result + receiver.result!! } } @@ -52,13 +52,13 @@ internal class SuspendableResultBindingImpl( ) : SuspendableResultBinding, CoroutineScope by delegate { private val mutex = Mutex() - lateinit var result: Err + var result: Result? = null override suspend fun Result.bind(): V { return when (this) { is Ok -> value is Err -> mutex.withLock { - if (::result.isInitialized.not()) { + if (result == null) { result = this coroutineContext.cancel(BindCancellationException) } 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 6a5024c..eac1db7 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 @@ -27,12 +27,12 @@ public inline fun binding(crossinline block: ResultBinding.() -> V): R callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - val receiver = ResultBindingImpl() - - return try { - with(receiver) { Ok(block()) } - } catch (ex: BindException) { - receiver.result + return with(ResultBindingImpl()) { + try { + Ok(block()) + } catch (ex: BindException) { + result!! + } } } @@ -45,7 +45,7 @@ public interface ResultBinding { @PublishedApi internal class ResultBindingImpl : ResultBinding { - lateinit var result: Err + var result: Result? = null override fun Result.bind(): V { return when (this) { 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 db3c9c7..c268620 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 @@ -22,11 +22,11 @@ public suspend inline fun binding(crossinline block: suspend ResultBindin callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - val receiver = ResultBindingImpl() - - return try { - with(receiver) { Ok(block()) } - } catch (ex: BindException) { - receiver.result + return with(ResultBindingImpl()) { + try { + Ok(block()) + } catch (ex: BindException) { + result!! + } } }