Replace lateinit with null in binding variants

This commit is contained in:
Michael Bull 2024-03-10 23:15:53 +00:00
parent fec8c03313
commit 0b21b7361f
3 changed files with 16 additions and 16 deletions

View File

@ -36,7 +36,7 @@ public suspend inline fun <V, E> binding(crossinline block: suspend SuspendableR
} }
} }
} catch (ex: BindCancellationException) { } catch (ex: BindCancellationException) {
receiver.result receiver.result!!
} }
} }
@ -52,13 +52,13 @@ internal class SuspendableResultBindingImpl<E>(
) : SuspendableResultBinding<E>, CoroutineScope by delegate { ) : SuspendableResultBinding<E>, CoroutineScope by delegate {
private val mutex = Mutex() private val mutex = Mutex()
lateinit var result: Err<E> var result: Result<Nothing, E>? = null
override suspend fun <V> Result<V, E>.bind(): V { override suspend fun <V> Result<V, E>.bind(): V {
return when (this) { return when (this) {
is Ok -> value is Ok -> value
is Err -> mutex.withLock { is Err -> mutex.withLock {
if (::result.isInitialized.not()) { if (result == null) {
result = this result = this
coroutineContext.cancel(BindCancellationException) coroutineContext.cancel(BindCancellationException)
} }

View File

@ -27,12 +27,12 @@ public inline fun <V, E> binding(crossinline block: ResultBinding<E>.() -> V): R
callsInPlace(block, InvocationKind.EXACTLY_ONCE) callsInPlace(block, InvocationKind.EXACTLY_ONCE)
} }
val receiver = ResultBindingImpl<E>() return with(ResultBindingImpl<E>()) {
try {
return try { Ok(block())
with(receiver) { Ok(block()) }
} catch (ex: BindException) { } catch (ex: BindException) {
receiver.result result!!
}
} }
} }
@ -45,7 +45,7 @@ public interface ResultBinding<E> {
@PublishedApi @PublishedApi
internal class ResultBindingImpl<E> : ResultBinding<E> { internal class ResultBindingImpl<E> : ResultBinding<E> {
lateinit var result: Err<E> var result: Result<Nothing, E>? = null
override fun <V> Result<V, E>.bind(): V { override fun <V> Result<V, E>.bind(): V {
return when (this) { return when (this) {

View File

@ -22,11 +22,11 @@ public suspend inline fun <V, E> binding(crossinline block: suspend ResultBindin
callsInPlace(block, InvocationKind.EXACTLY_ONCE) callsInPlace(block, InvocationKind.EXACTLY_ONCE)
} }
val receiver = ResultBindingImpl<E>() return with(ResultBindingImpl<E>()) {
try {
return try { Ok(block())
with(receiver) { Ok(block()) }
} catch (ex: BindException) { } catch (ex: BindException) {
receiver.result result!!
}
} }
} }