Skip to content

Instantly share code, notes, and snippets.

@adam-arold
Created September 11, 2017 16:31
Show Gist options
  • Save adam-arold/a4ffb5d187a893ac59af7b349ef399b3 to your computer and use it in GitHub Desktop.
Save adam-arold/a4ffb5d187a893ac59af7b349ef399b3 to your computer and use it in GitHub Desktop.
Kotlin user with nulls
data class KotlinUserWithNulls(val firstName: String?,
// String? means that it is either a String object or a null
val lastName: String?,
val addresses: List<Address> = listOf()) {
data class Address(val city: String?)
companion object {
fun fetchFirstCity(user: KotlinUserWithNulls?): String? {
user?.addresses?.forEach { it.city?.let { return it } }
return null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment