为什么解构声明不能用在 when 表达式中?
Why destructuring declaration cannot be used in when expression?
这个问题可能应该由 Kotlin 的作者提出,但我确信在 SO 上有许多对其架构有深入了解的 Kotlin 用户。
所以我的问题是:为什么该语言不支持 destructuring in when 表达式?
例如我想要以下代码:
data class Person(val name: String, val age: Int)
when (person) {
("John", _) -> print("It is John") //it won't compile
else -> print("It's not John")
}
由于解构使用 component1, component2, etc.
方法,我很好奇为什么不能像上面所示那样使用这种简单的值比较。是修改when
机制还是自毁问题?
有一个开放的功能票:
KT-20004: when 语句中类似 Scala 的构造函数模式匹配
此外,Java将在近期支持数据类和pattern matching,可能会对Kotlin 版本的实现。
这个问题可能应该由 Kotlin 的作者提出,但我确信在 SO 上有许多对其架构有深入了解的 Kotlin 用户。
所以我的问题是:为什么该语言不支持 destructuring in when 表达式?
例如我想要以下代码:
data class Person(val name: String, val age: Int)
when (person) {
("John", _) -> print("It is John") //it won't compile
else -> print("It's not John")
}
由于解构使用 component1, component2, etc.
方法,我很好奇为什么不能像上面所示那样使用这种简单的值比较。是修改when
机制还是自毁问题?
有一个开放的功能票:
KT-20004: when 语句中类似 Scala 的构造函数模式匹配
此外,Java将在近期支持数据类和pattern matching,可能会对Kotlin 版本的实现。