美文网首页
Kotlin编码窍门之解构声明(Destructuring De

Kotlin编码窍门之解构声明(Destructuring De

作者: 已迁至知乎_此不再维护 | 来源:发表于2017-06-02 19:22 被阅读0次

有时候,将一个对象解构为一系列变量将很方便,如:

val (name, age) = person 

这个语法称作解构声明。一个解构声明将同时创建多个变量。我们已经声明了两个新的变量:nameage,我们可以单独的使用它们:

println(name)
println(age)

一个解构声明被编译为以下代码:

val name = person.component1()
val age = person.component2()

函数component1()component2()函数是Kotlin中广泛使用的约定原则的另一个例子(请参阅运算符,如+和*,for循环等)。任何东西都可以置于解构声明的右侧,只要可以调用所需数量的组件函数。当然,可以有component3()component4()等等。

注意:组件函数componentN()需要被operator关键字标记,以允许在解构声明中使用它们。

解构声明也可以在for循环中使用,当你调用:

for ((a, b) in collection) { ... }

变量a和b将从集合元素的component1()component2()函数获取值。

例子:一个函数返回两个值(Example: Returning Two Values from a Function)

倘若我们需要从一个函数中返回两个值。例如,一个结果对象和一些排序状态。Kotlin中的简介方法是声明一个数据类并返回其实例:

data class Result(val result: Int, val status: Status)
fun function(...): Result {
    // computations
    
    return Result(result, status)
}

// Now, to use this function:
val (result, status) = function(...)

因为数据类会自动声明componentN()函数,因此解构声明可以在这里使用。

注意:我们也可以使用标准数据类Pair,并让function()函数返回Pair<Int, Status>,但通常使用自己命名的数据类会更好。

例子:解构声明和映射(Example: Destructuring Declarations and Maps)

遍历一个map的最好方法可能如下:

for ((key, value) in map) {
   // do something with the key and the value
}

为了使其能用,我们需要:

  1. 通过提供一个iterator()函数将map变为可遍历一系列值
  2. 通过提供component1()component2()函数将每一个元素作为kv对,

事实上,标准库提供了这样的扩展:

operator fun <K, V> Map<K, V>.iterator(): Iterator<Map.Entry<K, V>> = entrySet().iterator()
operator fun <K, V> Map.Entry<K, V>.component1() = getKey()
operator fun <K, V> Map.Entry<K, V>.component2() = getValue()

因此你可以在for循环中对map(和其他数据类实例的集合)使用解构声明。

未用变量以下划线代替(Underscore for unused variables (since 1.1))

如果在解构声明中没有用到某个变量,则可以使用下划线代替该变量的名称:

val (_, status) = getResult()

λ表达式中的解构(Destructuring in Lambdas (since 1.1))

可以为λ表达式的参数使用解构声明。如果一个λ表达式具有Pair类型(或Map.Entry和其他任意具有相应componentN函数的类型)的参数,则可以通过将它们放在括号中来引入几个新参数:

map.mapValues { entry -> "${entry.value}!" }
map.mapValues { (key, value) -> "$value!" }

注意声明两个参数 和 声明一个解构对代替一个参数的区别:

{ a -> ... } // one parameter
{ a, b -> ... } // two parameters
{ (a, b) -> ... } // a destructured pair
{ (a, b), c -> ... } // a destructured pair and another parameter

如果解构声明中的一个组件没有被使用,可以使用下划线代替其名字:

map.mapValues { (_, value) -> "$value!" }

也可以指定整个解构参数或解构中某一个参数(特定组件)的类型:

map.mapValues { (_, value): Map.Entry<Int, String> -> "$value!" }

map.mapValues { (_, value: String) -> "$value!" }

相关文章

网友评论

      本文标题:Kotlin编码窍门之解构声明(Destructuring De

      本文链接:https://www.haomeiwen.com/subject/wtgufxtx.html