قالب وردپرس درنا توس
Home / IOS Development / Kotlin Destructuring Declarations and ComponentN

Kotlin Destructuring Declarations and ComponentN

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 [1
9659021] 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.

Destructuring Collections

Using Kotlin's destructive syntax on collections, avoid assigning the same variables on mutliple lines that you will do in the example below

Example 1.

  choice    threeStrings    =    listOf   (  "one"  [19659009] "two    =    first    =    exampleList          others    others    others    =    exampleList   [  1 ] 

With such a convenient syntax, you might be crazy and extract all the values ​​of a
sixStrings = listOf to " " 19659009] " " "four" "five" "six" ]
choice ( first second second third ] fourt fifth sixth [19659006]) = exampleList [19659000] 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 component6 ()
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 . = = = = . = other = other others . component2

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
as threeStrings [0] you get access to a few properties on treStrings called
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 component1 or 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.

  Operator Fun  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
List initialize must have a & # 39; component6 () & # 39; -function.
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 > [19659005] Array   <  with    T  ]>   component1   ():    T 
  operator    fun    ByteArray .   ():   (): [1]   ():    Short [Arrray  [19659000]] [1]             ().    Short 
  short [operator]      IntArray     component1   () :.    Int 
  operator    fun    LongArray     component1   ():    Long 
  operator    fun    FloatArray   [Component1  ():    Float 
  operator    fun    DoubleArray     component1   (). [19659005] Double 
  operator    fun    BooleanArray     component1   ().    Boolean    moro    CharArray     ():    Char    moro [19659010]>    List   <  T >. Component 1   (): component 2 ()  -  component5 ()  looks the same as  component1 () 

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
of Numbers data class.

Data classes in Kotlin actually define a number of component N functions at compilation time,
where N is 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;

finally [
] [
] . second.

( ]

] Conclusion

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!

Source link