Skip to content

Instantly share code, notes, and snippets.

@v3rm0n
Created May 16, 2018 12:04
Show Gist options
  • Save v3rm0n/852b42301fa79dce83520c4899625912 to your computer and use it in GitHub Desktop.
Save v3rm0n/852b42301fa79dce83520c4899625912 to your computer and use it in GitHub Desktop.
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