Created
May 16, 2018 12:04
-
-
Save v3rm0n/852b42301fa79dce83520c4899625912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val user: User? | |
// Get user ID | |
//If expression (userId is nullable) | |
val userId = if(user != null) user.id else null | |
//Elvis operator (userId is not nullable, fail fast) | |
val userId = user.id ?: throw IllegalStateException("User can not be null at this time") | |
//Any?.let (userId is nullable) | |
val userId = user?.let{it.id} | |
//Any?.let (userId is not nullable, is a String, can be empty) | |
val userId = user?.let{it.id}.orEmpty() | |
//Safe navigation (userId is nullable) | |
val userId = user?.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment