Kotlin magic shorthand sytax is everywhere! Kotlin adds some useful syntactic sugar
assign values from structures to multiple variables simultaneously, as in the example below:
val threeStrings = listOf ( "one" "to" " three ") choice ( first second ) = exampleList  In this article we will review how it is possible in Kotlin, and take a look at two very common places this destructive syntax can be used.
Using Kotlin's destructive syntax on collections, avoid assigning the same variables on mutliple lines that you will do in the example below
choice threeStrings = listOf ( "one"  "two = first = exampleList others others others = exampleList [ 1 ]
With such a convenient syntax, you might be crazy and extract all the values of asixStrings = listOf to " " 19659009] " " "four" "five" "six" ]
choice ( first second second third ] fourt fifth sixth ) = exampleList  but you will notice that if you try the above example, the compiler complains and asks the
you with an error on
Kotlin: List [DestructionDeclarationInitialsinitials.
must have a & # 39; component6 () & # 39; function
What gives! Why don't the code collect and why do you ask the error to call someone= = = = . = other = other others . component2
Works if it worked to get the items you wanted in your previous example? To see
What happens, let's look at what the Kotlin compiler does when compiling the code in Example 1 .
Here you see your first paragraph in finding out why you get the mysterious error.
The first thing you might note here is that Kotlin has converted your short hand syntax into
a long form. But instead of indexing the list in a way that you might be used to seeing, so
threeStrings  you get access to a few properties on
and component 2 . If you actually go ahead and look at the implementation of
Your list of type
List you can note that none of
component2 is defined as characteristics of the class.
What the creators of Kotlin did was actually implement expansion functions on many of the classes in the
Collections.kt library that we commonly use.
List .component1 (): T operator fun List .component2 (): T operator fun List .component3 (): T operator fun List .component4 (): T operator pleasure List .component5 (): T
Under the hood, Kotlin actually calls
the component function of the fifth element
that you will withdraw from your collection. That is why in Example 2 you got this
error when trying to pull six items out of the list.
Kotlin: Declaration of Destruction Kotlin didn't
actually implement an expansion function to extract component6 () .
If you look at the explicit documentation for
component1 () with the only difference between itself, you can actually see that it is really implemented on Kotlin Collections not just one
List as in our example:
operator funny < T >  Array < with T ]> component1 (): T operator fun ByteArray . (): ():  (): Short [Arrray ]  (). Short short [operator] IntArray component1 () :. Int operator fun LongArray component1 (): Long operator fun FloatArray [Component1 (): Float operator fun DoubleArray component1 ().  Double operator fun BooleanArray component1 (). Boolean moro CharArray (): Char moro > List < T >. Component 1 (): component 2 ()-
component5 ()looks the same as
Destructuring Data classes
As with collections, you can also destroy the properties of your data classes
data class Numbers ( choice first : String String String String ] Choice second third : String third fourth : String fall fifth : String ] String ] ( on ] ] ] ] 19659005] fem sex ) = number ( "and" "to" "three " " four " " five " " six ")
With the data class, you can actually compile this code even if you try to draw
out 6 items. Why then you were given a mistake trying to do the same with a
collection? Data class destructuring seems a little different than destructuring
collections. When you look at what Kotlin does when compiled, you will notice it
that it actually made a component for each property that was submitted to the designer
Data classes in Kotlin actually define a number of
component Nfunctions at compilation time,
Nis the amount of values you enter into the designer. Because these are not
expansion features that are part of the standard Kotlin library, but features that are
generated by Kotlin, if you were to decompile this data class, you can actually see everyone
) return . first;
] . second.
You now know what happens to the componentN under the cap.
We have destroyed the magic and shown that Kotlin only works under the hood to allow us
to extract the values that we want. If you have any questions, I would like to hear from you.
You may like to reach @rmorenocesar or comment below!