July 2018
Intermediate to advanced
242 pages
8h 6m
English
Because of the fact that properties in destructured object assignments are position-based, sometimes we are forced to declare more variables than we want to use. We can use an underscore if we don't need to use a certain value, avoiding the compiler hint indicating an unused variable and simplifying the code a bit:
val (_, turnedOn) = LightBulb(1)print("Light bulb is turned ${if (turnedOn) "on" else "off"}")
Destructuring is also available for function return values:
val (login, domain) = "agata@magdalena.com".split("@")print("login: $login, domain: $domain")
The preceding code is going to return the following output:
login: agata, domain: magdalena.com
We can also use destructured declarations with lambda expressions:
listOf ...Read now
Unlock full access