Files
kotlin-fork/compiler/testData/codegen/boxInline/reified/checkCast/kt26435.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
738 B
Kotlin
Vendored

// WITH_STDLIB
// FILE: 1.kt
package test
enum class Id {
OK,
FAIL
}
sealed class Base(val id: Id)
class A(id: Id) : Base(id)
class B(id: Id) : Base(id)
inline fun <reified T : Base> process(t: T, f: (T) -> Unit): Base? {
f(t)
return getSomeBaseObject(t.id) as? T ?: throw RuntimeException()
}
fun getSomeBaseObject(id: Id): Base = if (id == Id.OK) A(id) else B(id)
// FILE: 2.kt
import test.*
fun doSth(base: Base): Base? =
if (base is A) process(base, f = ::doSomethingInCaseOfA)
else if (base is B) process(base, f = ::doSomethingInCaseOfB) else error("123")
fun doSomethingInCaseOfA(a: A) {}
fun doSomethingInCaseOfB(b: B) {}
fun box(): String {
val a = doSth(A(Id.OK))!!
return a.id.name
}