Created
May 24, 2022 03:40
-
-
Save Guilherme-Marcionilo/4cf7bda984bb3cc05f92573636a4e01d to your computer and use it in GitHub Desktop.
Exemplos de Destructuring declarations - Kotlin
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
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) |
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
class Pessoa(val nome: String, val idade: Int) { | |
operator fun component1(): String = nome | |
operator fun component2(): Int = idade | |
} | |
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
fun main() { | |
//código omitido | |
//underscore | |
val (_, idade) = pessoa | |
println("$idade") | |
//RESULTADO: 19 | |
} |
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
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