kotlin destructuring statement

1. Concept

Kotlin Destructuring Declarations is a convenient syntax feature that can decompose a complex data structure (such as an object or list) into multiple variables , and then use these variables separately.

In Kotlin, you can use the following syntax to perform destructuring declarations:

val (var1, var2, ...) = obj

where obj is the object to be destructured, and var1, var2, etc. are the variables to be assigned to its properties. The number of these variables must be the same as the number of properties of the object.

For example:

data class Person(val name: String, val age: Int)
val person = Person("Kevin", 29)
// 1. 两个变量与对象的两个属性数量相同
// 2. 顺序要与对象的属性一一对应
val (name, age) = person

println("Name: $name, Age: $age") // 输出 "Name: Kevin, Age: 29"

Here, we define a Persondata class named , which has two properties: nameand age. Then, we created an Personobject personand assigned its properties to the nameand agevariables using destructuring declaration.

In addition to object properties, destructuring declarations can also be used for lists and other data types, for example:

val list = listOf("apple", "banana", "orange")
val (first, second, third) = list

println("First item: $first, Second item: $second, Third item: $third") // 输出 "First item: apple, Second item: banana, Third item: orange"

Here, we listdecompose a string list into three variables first, secondand third.

2. Summary

  1. The number of variables must be the same as the number of properties of the object . If they are not the same, a compile-time error will occur.

  2. Destructuring declarations can be used for any type of object , including arrays, lists, maps, etc. However, in order to use destructuring declarations correctly, the object must have a corresponding number of properties or elements.

  3. For destructuring declared variables, you can optionally use an underscore (_) to indicate an attribute or element that you don't care about . For example:

    val (_, age) = person
    

In this example, we use a destructuring declaration to assign the second property of the Person object (age) to the age variable. Since we don't care about the first attribute of the object (the name), we use an underscore to ignore it.

  1. The order of properties of an object is very important . When performing a destructuring declaration, the order of the variables must match the order of the object's properties. Otherwise, you will get wrong results.

  2. If you want to use destructuring declarations in a class, the class must implement the componentN() function. These functions are usually automatically generated by the compiler, but if you want to implement these functions yourself, you need to follow the naming convention. For example

    class Person(val name: String, val age: Int) {
          
          
        operator fun component1() = name
        operator fun component2() = age
    }
    

Guess you like

Origin blog.csdn.net/flytosky21/article/details/130071994