Skip to content

Instantly share code, notes, and snippets.

@Guilherme-Marcionilo
Created May 24, 2022 03:40
Show Gist options
  • Save Guilherme-Marcionilo/4cf7bda984bb3cc05f92573636a4e01d to your computer and use it in GitHub Desktop.
Save Guilherme-Marcionilo/4cf7bda984bb3cc05f92573636a4e01d to your computer and use it in GitHub Desktop.
Exemplos de Destructuring declarations - Kotlin
fun main() {
val pessoa = Pessoa("Gui", 19)
val (nome, idade) = pessoa
println("$nome $idade")
//RESULTADO: Gui 19
}
data class Pessoa(val nome: String, val idade: Int)
class Pessoa(val nome: String, val idade: Int) {
operator fun component1(): String = nome
operator fun component2(): Int = idade
}
fun main() {
//código omitido
//underscore
val (_, idade) = pessoa
println("$idade")
//RESULTADO: 19
}
fun main() {
val pessoa = Pessoa("Gui", 19, "SP")
val (nome, cidade) = pessoa
println("$nome $cidade")
//RESULTADO: Gui 19
}
data class Pessoa(val nome: String, val idade: Int, val cidade: String)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment