[history] Crossreturn check with error
This commit is contained in:
parent
1b9bf5e49f
commit
fc4bd9eb89
@ -1,18 +1,16 @@
|
||||
package ru.landgrafhomyak.utility.highlevel_try_finally
|
||||
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
||||
|
||||
inline fun <R> safeAutoClose(onError: () -> Unit = {}, onSuccess: () -> Unit = {}, action: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
|
||||
callsInPlace(onError, InvocationKind.AT_MOST_ONCE)
|
||||
callsInPlace(onSuccess, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
fun <R> safeAutoClose(
|
||||
onError: () -> Unit = {},
|
||||
onSuccess: () -> Unit = {},
|
||||
action: () -> R
|
||||
): R {
|
||||
val ret: R
|
||||
var wasError = false
|
||||
var crossReturned = true
|
||||
try {
|
||||
ret = action()
|
||||
crossReturned = false
|
||||
} catch (e1: Throwable) {
|
||||
wasError = true
|
||||
try {
|
||||
@ -22,8 +20,11 @@ inline fun <R> safeAutoClose(onError: () -> Unit = {}, onSuccess: () -> Unit = {
|
||||
}
|
||||
throw e1
|
||||
} finally {
|
||||
if (!wasError)
|
||||
if (!wasError) {
|
||||
if (crossReturned)
|
||||
throw Error("crossreturn")
|
||||
onSuccess()
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
Loading…
Reference in New Issue
Block a user