The Programmers Guide To Kotlin - Destructuring |
Written by Mike James | |||
Tuesday, 15 February 2022 | |||
Page 1 of 2 Destructuring - it sounds like something to avoid, but in practice it is really useful. In this extract from The Programmers Guide To Kotlin by Mike James, we look at destructuring and the spread operator. . Programmer's Guide To Kotlin Third Edition
You can buy it from: Amazon Contents
<ASIN:B0D8H4N8SK> In chapter but not in this extract
DestructuringDestructuring is a sophisticated-sounding name for a very simple idea. Sometimes you have a data structure with a number of elements or properties and you want to unpack these into a set of standalone variables. For example, if we have a data class: data class MyDataClass(var name:String,var age:Int) then we can create an instance and use destructuring to pack the data into separate variables: var myDataObject=MyDataClass("Mickey",89) var (myName,myAge) = myDataObject println(myName) println(myAge) The destructuring assignment: var (myName,myAge) = myDataObject is converted into: var myName = myDataObject.component1() var myAge = myDataObject.component2() For a data object the operator methods component1, component2 and so on are automatically generated. This is the reason why, if you take the data modifier from the start of the declaration, you will get an error message as the class no longer has componentN methods. Of course, you can add them manually: class MyDataClass(var name:String,var age:Int){ In fact any class that has componentN operator methods can be used in a destructuring operation. As well as assignment destructuring can be used in for loops. For example: val myList=listOf(MyDataClass("Mickey",89), As long as each element that the iterator returns supports destructuring, you can use this sort of for loop. Destructuring can also allow a function to seem to return multiple values. Following: fun myFavouriteMouse():MyDataClass{ return MyDataClass("Mickey",89) } you can now write: var (myName,myAge) = myFavouriteMouse() which looks as if myFavouriteMouse returns multiple values in the same way as in languages such as Python. If you don't want to make use of a destructuring value you can use an underscore to leave it out: var (_,myAge) = myFavouriteMouse() Finally, you can use destructuring in parameters of lambdas as long as the parameter type has componentN operator functions. Notice that this makes adding an extra set of parenthesis in a lambda an error. That is: { a,b -> statements that use a and b} {(a,b)-> statements that use a and b} are different in that the second one accepts a single parameter that can be destructured to give a and b. For example: val myLambda= {(name,age):MyDataClass-> println(name) println(age) } This looks like a lambda that would accept two parameters, but because of destructuring it accepts a single instance of MyDataClass: val myDataObject=MyDataClass("Mickey",89) myLambda(myDataObject) This works with any class that supports componentN operator methods.
|
|||
Last Updated ( Tuesday, 15 February 2022 ) |