[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
|
package ru.landgrafhomyak.utility.highlevel_try_finally
|
||||||
|
|
||||||
import kotlin.contracts.InvocationKind
|
fun <R> safeAutoClose(
|
||||||
import kotlin.contracts.contract
|
onError: () -> Unit = {},
|
||||||
|
onSuccess: () -> Unit = {},
|
||||||
inline fun <R> safeAutoClose(onError: () -> Unit = {}, onSuccess: () -> Unit = {}, action: () -> R): R {
|
action: () -> R
|
||||||
contract {
|
): R {
|
||||||
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
|
|
||||||
callsInPlace(onError, InvocationKind.AT_MOST_ONCE)
|
|
||||||
callsInPlace(onSuccess, InvocationKind.EXACTLY_ONCE)
|
|
||||||
}
|
|
||||||
val ret: R
|
val ret: R
|
||||||
var wasError = false
|
var wasError = false
|
||||||
|
var crossReturned = true
|
||||||
try {
|
try {
|
||||||
ret = action()
|
ret = action()
|
||||||
|
crossReturned = false
|
||||||
} catch (e1: Throwable) {
|
} catch (e1: Throwable) {
|
||||||
wasError = true
|
wasError = true
|
||||||
try {
|
try {
|
||||||
@ -22,8 +20,11 @@ inline fun <R> safeAutoClose(onError: () -> Unit = {}, onSuccess: () -> Unit = {
|
|||||||
}
|
}
|
||||||
throw e1
|
throw e1
|
||||||
} finally {
|
} finally {
|
||||||
if (!wasError)
|
if (!wasError) {
|
||||||
|
if (crossReturned)
|
||||||
|
throw Error("crossreturn")
|
||||||
onSuccess()
|
onSuccess()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user