Kotlin: smart casts
Example:
fun length(any: Any): Int? {
if (any is String) {
return any.length
}
return null
}
Kotlin's smart casts eliminate explicit type casting.
Solution:
print(length("Hello")) // Outputs: 5
Example: Solution:Kotlin: smart casts
Kotlin's smart casts eliminate explicit type casting.
fun length(any: Any): Int? {
if (any is String) {
return any.length
}
return null
}
print(length("Hello")) // Outputs: 5