Skip to content

Commit

Permalink
Replace lateinit with null in binding variants
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbull committed Mar 10, 2024
1 parent 0f21774 commit 169b984
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public suspend inline fun <V, E> binding(crossinline block: suspend SuspendableR
}
}
} catch (ex: BindCancellationException) {
receiver.result
receiver.result!!
}
}

Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!!
}
}
}

Expand All @@ -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) {
Expand Down

0 comments on commit 169b984

Please sign in to comment.