fun getStringLength(obj: Any): Int? { // `obj`...
# getting-started
c
fun getStringLength(obj: Any): Int? { //
obj
is automatically cast to
String
on the right-hand side of
&&
if (obj is String && obj.length > 0) { return obj.length } return null } Why is it Int here?
s
Int being the return type, the
?
basically says “forget the type, I can be null too”
So,
fun getStringLength(obj: Any): <Int or null> {
c
got it , it just made sense, I was reading it as returning String or null. Got my self confused. I just saw it was returning length of String which is Int 🤦‍♂️ . Thanks.
s
You’re welcome!
a
@crazdrms for future occasions, please surround multi-line code with 3 backticks
c
Will do.Thanks.
👍 1