Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// WITH_STDLIB
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

interface MyDeferred<T> {
suspend fun await(): T
}

abstract class MyException : Exception() {
abstract fun isInternal(): Boolean
}

@OptIn(ExperimentalContracts::class)
suspend fun <T> MyDeferred<T>.safeAwait(
fallbackOnAbort: suspend () -> T,
onCancelled: suspend (MyException) -> T = { throw it },
) : T {
contract <expr>{
callsInPlace(fallbackOnAbort, InvocationKind.AT_MOST_ONCE)
callsInPlace(onCancelled, InvocationKind.AT_MOST_ONCE)
}</expr>
return try {
await()
} catch (e: MyException) {
if (e.isInternal()) {
fallbackOnAbort()
} else {
onCancelled(e)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
expression: {
callsInPlace(fallbackOnAbort, InvocationKind.AT_MOST_ONCE)
callsInPlace(onCancelled, InvocationKind.AT_MOST_ONCE)
}
type: kotlin.contracts.ContractBuilder.() -> kotlin.Unit