From the course: Kotlin for Android: Best Practices
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Scope functions: let - Kotlin Tutorial
From the course: Kotlin for Android: Best Practices
Scope functions: let
- [Instructor] We're going to explore more how to deal with null in Kotlin. I'm going to switch gears and use the Kotlin Playground instead of Android Studio. I have saved the beginning of this program into the exercise file. Open it, and copy and paste that into Kotlin Playground. Let's take a look at this code. It defines a user, which may have a birthplace, or may be null. Birthplace is a place, which can contain a country and a city. So in main, we have two users, Alice, whose birthplace is United States for the country, and Houston for the city, and Bob, we just know that he's born in Japan and we don't know which city. Let's say we want to print the birthplace for a user. So we have a user, and let's say this user is Alice. And then, we will print the user's birthplace and we want to know the city. Notice that I put a question mark after birthplace. If I don't, Kotlin's going to be not happy because hey, birthplace could be null, what do I do when it's null? Earlier, you saw…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.