Blog: Safe & Smart - Type Casts and Checks in Kotlin
Day 31! Today I explored Type Checks and Casts in Kotlin. If you come from a language where you constantly have to write redundant type conversions after checking them, Kotlin will feel like magic.
The Power of Smart Casts
In most languages, if you check if an object is a String, you still have to explicitly cast it to a String before calling methods on it. Not in Kotlin! The compiler is smart enough to do this automatically.
if (obj is String) {
println(obj.length) // No manual cast needed!
}
It even works across logical operators like && or inside when expressions. It keeps your code unbelievably clean.
Unsafe vs. Safe Casts
When you do need to cast explicitly, Kotlin gives you two choices:
1. The Unsafe Cast (as): Forces a cast. If it's the wrong type, it crashes with a ClassCastException.
2. The Safe Cast (as?): Tries to cast, but if it fails, it just returns null instead of crashing.
Safe casts combined with the Elvis operator (?:) make error handling extremely concise and safe.
Generic Type Erasure
One interesting catch I learned about is Type Erasure. At runtime, a List<String> and a List<Int> both look exactly the same to the JVM—just a List. Because of this, you can't check is List<String>. Instead, you use a star projection like is List<*> to check if it's a list, and then cast it explicitly.
Summary
Kotlin takes the headache out of type conversions by letting the compiler do the heavy lifting while giving you elegant, crash-proof safe cast operators when you need explicit control.
Check out the full technical breakdown!
Comments
Post a Comment