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

Scope functions: let - Kotlin Tutorial

From the course: Kotlin for Android: Best Practices

Start my 1-month free trial

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…

Contents