Kotlin Code Pills — Safe Calls
Published in
1 min readApr 19, 2019
In Kotlin , one of the option is the safe call operator, written with ?.
var b: String? = nullprintln(b?.length)
Prints b.lenght
if b
is not null, otherwise prints null.
Safe calls are useful for chains like
person?.address?.city
If any field of the chain is null, returns null.
Another option, if you are sure that the nullable variable definitely won’t be null. Than you can use !!
.
val city = person?.address?.city ?: "Istanbul"
var result = location[city]!!
I am 100% confident that result
is a non-null entry.
See you next Code pills.