Files
kotlin-fork/compiler/testData/codegen/box/contracts/kt45236.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

35 lines
882 B
Kotlin
Vendored

// WITH_STDLIB
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
sealed class NetRequestStatus<out T : Any> {
abstract val value: T?
data class Error<out T : Any>(
val error: Throwable,
override val value: T? = null,
) : NetRequestStatus<T>()
}
@OptIn(ExperimentalContracts::class)
fun <T : Any> NetRequestStatus<T>.isError(): Boolean {
contract { returns(true) implies (this@isError is NetRequestStatus.Error) }
return (this is NetRequestStatus.Error)
}
fun <T : Any> successOrThrow() {
val nextTerminal: NetRequestStatus<T> = NetRequestStatus.Error(Exception())
if (nextTerminal.isError()) throw nextTerminal.error
}
fun box(): String {
try {
successOrThrow<String>()
} catch (e: Exception) {
return "OK"
}
return "'successOrThrow<...>()' should throw an exception"
}