Files
kotlin-fork/backend.native/tests/codegen/basics/unchecked_cast3.kt
T
2017-01-23 17:18:43 +07:00

35 lines
640 B
Kotlin

fun main(args: Array<String>) {
testCast<Test>(Test(), true)
testCast<Test>(null, false)
testCastToNullable<Test>(null, true)
println("Ok")
}
class Test
fun ensure(b: Boolean) {
if (!b) {
println("Error")
}
}
fun <T : Any> testCast(x: Any?, expectSuccess: Boolean) {
try {
x as T
} catch (e: Throwable) {
ensure(!expectSuccess)
return
}
ensure(expectSuccess)
}
fun <T : Any> testCastToNullable(x: Any?, expectSuccess: Boolean) {
try {
x as T?
} catch (e: Throwable) {
ensure(!expectSuccess)
return
}
ensure(expectSuccess)
}