Programming Kotlin by 2017
Author:2017
Language: eng
Format: epub, mobi
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.
Deep Learning with Python by François Chollet(12569)
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)
Dependency Injection in .NET by Mark Seemann(9337)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
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)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7059)
Microservices with Go by Alexander Shuiskov(6824)
Practical Design Patterns for Java Developers by Miroslav Wengner(6741)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6680)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6086)
The Art of Crafting User Stories by The Art of Crafting User Stories(5615)
NetSuite for Consultants - Second Edition by Peter Ries(5554)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5353)
Kotlin in Action by Dmitry Jemerov(5062)
