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
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.
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8295)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6820)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6549)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6416)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(3993)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3784)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3726)
The Ultimate iOS Interview Playbook by Avi Tsadok(3702)
