Programming Kotlin by Stephen Samuel

Programming Kotlin by Stephen Samuel

Author:Stephen Samuel
Language: eng
Format: epub
Publisher: Packt Publishing


Note

A null smart cast only works when the variable is either a member val without a backing field, a local val, or a local var that is not mutated between the check and the usage. Otherwise, the variable might be non-null when it's checked and then changed to null before we use it, thus throwing an exception. The compiler will enforce this restriction.

Safe null access

Smart casts are a very nice feature, and offer a readable way to do branching when dealing with nulls. However, when we have chained operations, and each step may produce a null, the code quickly becomes unreadable.

Consider the following snippet:

class Person(name: String, val address: Address?) class Address(name: String, postcode: String, val city: City?) class City(name: String, val country: Country?) class Country(val name: String) fun getCountryName(person: Person?): String? { var countryName: String? = null if (person != null) { val address = person.address if (address != null) { val city = address.city if (city != null) { val country = city.country if (country != null) { countryName = country.name } } } } return countryName }

Look at the levels of nested if-not-null checks required! It's easy to imagine even more levels of nesting required in some scenarios. So can we do better?

With Kotlin we can. The alternative to smart casts is to use the safe null access operator. This is similar to the normal dot syntax for functions and properties, but uses ?. When using this operator, the compiler will automatically insert the null check for us to ensure that we don't access a null accidentally. So the previous example can be re-written as the following:

fun getCountryNameSafe(person: Person?): String? { return person?.address?.city?.country?.name }

The difference is striking. If we examine the bytecode generated for this function, we can see that the compiler is indeed inserting the null checks:

public static final java.lang.String getCountryNameSafe(Person); Code: 0: aload_0 1: dup 2: ifnull 32 5: invokevirtual #15 // Method Person.getAddress:()LAddress; 8: dup 9: ifnull 32 12: invokevirtual #21 // Method Address.getCity:()LCity; 15: dup 16: ifnull 32 19: invokevirtual #27 // Method City.getCountry:()LCountry; 22: dup 23: ifnull 32 26: invokevirtual #33 // Method Country.getName:()Ljava/lang/String; 29: goto 34 32: pop 33: aconst_null 34: areturn

The key instructions here are 2, 9, 16, and 23, which show the compiler performing a null check and if-null jumping to instruction 32, before adding a null to the stack to be returned.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.