Safe call operator

Another way of accessing a method or property of a nullable type is by using the safe call operator:

val len = gender?.length
print("Length of gender is $len")

In the preceding code, if the gender is not null, the value of len will be the result of gender.length. Otherwise, the value of len will be null.

Using the safe call operator is great if you don't need to perform any actions when gender is null. What if we want to assign len a different value when gender is null? We can combine the safe call operator with the Elvis operator for that.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset