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) {
receiver.result
receiver.result!!
}
}
@ -52,13 +52,13 @@ internal class SuspendableResultBindingImpl<E>(
) : SuspendableResultBinding<E>, CoroutineScope by delegate {
private val mutex = Mutex()
lateinit var result: Err<E>
var result: Result<Nothing, E>? = null
override suspend fun <V> Result<V, E>.bind(): V {
return when (this) {
is Ok -> value
is Err -> mutex.withLock {
if (::result.isInitialized.not()) {
if (result == null) {
result = this
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)
}
val receiver = ResultBindingImpl<E>()
return try {
with(receiver) { Ok(block()) }
} catch (ex: BindException) {
receiver.result
return with(ResultBindingImpl<E>()) {
try {
Ok(block())
} catch (ex: BindException) {
result!!
}
}
}
@ -45,7 +45,7 @@ public interface ResultBinding<E> {
@PublishedApi
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 {
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)
}
val receiver = ResultBindingImpl<E>()
return try {
with(receiver) { Ok(block()) }
} catch (ex: BindException) {
receiver.result
return with(ResultBindingImpl<E>()) {
try {
Ok(block())
} catch (ex: BindException) {
result!!
}
}
}