What if your program could remember what type something is, so you never have to convert it twice?
Why Smart casts in when and if in Kotlin? - Purpose & Use Cases
Imagine you have a box that might contain different types of items, like a toy or a book. To use the item properly, you need to check its type first and then tell the program exactly what it is before you can play or read it.
Manually checking the type and then converting it every time is slow and tiring. You might forget to convert it, causing errors, or write the same checks again and again, making your code messy and hard to fix.
Smart casts in when and if let the program automatically understand the type after you check it once. This means you can use the item directly without extra steps, making your code cleaner and safer.
if (obj is String) { val str = obj as String println(str.length) }
if (obj is String) {
println(obj.length)
}It lets you write simpler and safer code by trusting the program to remember type checks and avoid unnecessary conversions.
When reading user input that could be a number or text, smart casts help you handle each case easily without extra code to convert types again and again.
Manual type checks require extra conversions that slow you down.
Smart casts automatically remember type checks in when and if.
This makes your code cleaner, safer, and easier to read.
Practice
if or when block?Solution
Step 1: Understand smart cast purpose
Smart casts let Kotlin automatically treat a variable as a more specific type after you check its type.Step 2: Apply to if/when blocks
Insideiforwhen, Kotlin knows the variable's type and lets you use it without manual casting.Final Answer:
Automatically treats a variable as a specific type after a type check -> Option CQuick Check:
Smart cast = automatic type use after check [OK]
- Thinking manual cast is always needed
- Assuming smart casts disable type checks
- Believing variables convert to string automatically
when expression for variable obj?Solution
Step 1: Check correct when syntax
Thewhenexpression requires parentheses around the variable:when (obj).Step 2: Use type check correctly
The correct type check isis Stringwithout repeating variable name inside the block.Final Answer:
when (obj) { is String -> println(obj.length) else -> println("Not a string") } -> Option DQuick Check:
Correct when syntax uses parentheses andis[OK]
- Omitting parentheses around variable
- Using variable name inside when branches
- Using type name without 'is' keyword
fun test(obj: Any) {
when (obj) {
is String -> println(obj.length)
is Int -> println(obj + 1)
else -> println("Unknown")
}
}
test("Hello")
test(5)
test(3.14)Solution
Step 1: Analyze each call
Fortest("Hello"),objis String, so prints length 5.
Fortest(5),objis Int, so prints 5 + 1 = 6.
Fortest(3.14), no matching type, prints "Unknown".Step 2: Confirm output lines
Outputs are:
5
6
UnknownFinal Answer:
5 6 Unknown -> Option BQuick Check:
Smart cast uses type to choose output [OK]
- Confusing string length with string itself
- Adding 1 to string instead of int
- Expecting default else to print number
fun check(obj: Any) {
if (obj is String) {
println(obj.length)
} else if (obj is Int) {
println(obj + 1)
} else {
println(obj.length)
}
}Solution
Step 1: Review else block usage
In the else block,objcan be any type, but code tries to accessobj.lengthwhich is invalid for non-String types.Step 2: Understand smart cast limits
Smart casts only apply after type checks. Here, no type check before accessinglengthin else block causes error.Final Answer:
Trying to access length on obj in else block without type check -> Option AQuick Check:
Access property only after type check [OK]
- Assuming else block has smart cast automatically
- Confusing else if with when
- Thinking smart casts fail inside if blocks
Any objects, write a Kotlin function using when with smart casts to return a list of strings describing each element's type and value. What is the correct implementation?fun describeList(items: List<Any>): List<String> {
return items.map { item ->
when (item) {
is String -> "String of length ${'$'}{item.length}"
is Int -> "Int with value ${'$'}item"
else -> "Unknown type"
}
}
}Solution
Step 1: Check smart cast usage in when
Thewhenexpression smart castsitemtoStringorIntautomatically after type check.Step 2: Confirm lambda and map usage
Inside the lambda, smart casts work normally, so accessingitem.lengthanditemis valid without explicit casts.Final Answer:
Correct as is, smart casts allow using item.length and item directly -> Option AQuick Check:
Smart casts work inside lambdas and when [OK]
- Adding unnecessary explicit casts
- Thinking smart casts don't work in lambdas
- Replacing when with if-else unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
