Skip to content
Justice Tech School

Smart Casts

After an is check the compiler treats the value as that type.

Example
fun len(x: Any) = if (x is String) x.length else -1
println(len("hello"))
println(len(42))
val v: Any = 7
if (v is Int && v > 5) println("big int")

Exercise

Type check.

x  String

Quiz Test

What is a smart cast?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.