Safe calls

Safe calls let you access methods and properties of nullable values if the value isn't null (under the hood, at the bytecode level, a safe call is transformed into if(x != null)):

nullableCupcake?.eat()

But, what if you use it in an expression?

val result: String? = nullableCupcake?.eat()

It will return null if our value is null, so result must have a String? type.

That opens up the chance to use safe calls on a chain, as follows:

val length: Int? = nullableCupcake?.eat()?.length
..................Content has been hidden....................

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