Files
kotlin-fork/compiler/testData/codegen/box/contracts/kt47300.kt
T
Ivan Kylchik c7435ba760 Replace all occurrences of WITH_RUNTIME with WITH_STDLIB
We are going to deprecate `WITH_RUNTIME` directive. The main reason
behind this change is that `WITH_STDLIB` directive better describes
its meaning, specifically it will add kotlin stdlib to test's classpath.
2021-11-17 15:26:38 +03:00

38 lines
876 B
Kotlin
Vendored

// !OPT_IN: kotlin.contracts.ExperimentalContracts
// WITH_STDLIB
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
data class Content<out T>(val value: T)
fun <T> content(value: T) = Content(value)
@ExperimentalContracts
inline fun <R, T : R> Content<T>.getOrElse(
onException: (exception: Exception) -> R,
): R = fold({ it }, onException)
@ExperimentalContracts
inline fun <R, T> Content<T>.fold(
onContent: (value: T) -> R,
onException: (exception: Exception) -> R,
): R {
contract {
callsInPlace(onContent, InvocationKind.AT_MOST_ONCE)
callsInPlace(onException, InvocationKind.AT_MOST_ONCE)
}
return onContent(value)
}
@ExperimentalContracts
fun box(): String {
val t = content(1).getOrElse { 2 }
if (t != 1) return "Failed: $t"
return "OK"
}