Kotlin 中元组的模式匹配
Pattern matching on tuples in Kotlin
我有如下一段代码
fun changeSelection(item: FileOrFolder, selected: Boolean) {
selection = when(item) {
is Folder -> {
when(selected) {
true -> selection + item
false -> selection - item
}
}
is File -> {
when(selected) {
true -> selection + item
false -> selection - item
}
}
else -> throw Exception("unreachable")
}
}
这对我来说有点混乱,因为我已经习惯了 scala
def changeSelection(item: FileOrFolder, selected: Boolean) {
(item, selected) match {
case (item: Folder, true) => selection + item
case (item: Folder, false) => selection - item
case (item: File, true) => selection + item
case (item: File, false) => selection - item
}
}
有没有办法在 Kotlin 中实现类似的功能?
好吧,为了 Scala,你可以这样写:
fun changeSelection(item: FileOrFolder, selected: Boolean) {
selection = when {
item is Folder && selected -> selection + item
item is Folder && !selected -> selection - item
item is File && selected -> selection + item
item is File && !selected -> selection - item
else -> throw Exception()
}
}
更直接的版本是:
fun changeSelection(item: FileOrFolder, selected: Boolean) {
selection = if(selected)
when(item) {
is Folder -> selection + item
is File -> selection + item
else -> throw Exception()
}
else
when(item) {
is Folder -> selection - item
is File -> selection - item
else -> throw Exception()
}
}
但我绝对更喜欢:
operator fun Selection.plus(f: FileOrFolder) = ...
operator fun Selection.minus(f: FileOrFolder) = ...
fun changeSelection(item: FileOrFolder, selected: Boolean) {
if(selected)
selection += item
else
selection -= item
}
我有如下一段代码
fun changeSelection(item: FileOrFolder, selected: Boolean) {
selection = when(item) {
is Folder -> {
when(selected) {
true -> selection + item
false -> selection - item
}
}
is File -> {
when(selected) {
true -> selection + item
false -> selection - item
}
}
else -> throw Exception("unreachable")
}
}
这对我来说有点混乱,因为我已经习惯了 scala
def changeSelection(item: FileOrFolder, selected: Boolean) {
(item, selected) match {
case (item: Folder, true) => selection + item
case (item: Folder, false) => selection - item
case (item: File, true) => selection + item
case (item: File, false) => selection - item
}
}
有没有办法在 Kotlin 中实现类似的功能?
好吧,为了 Scala,你可以这样写:
fun changeSelection(item: FileOrFolder, selected: Boolean) {
selection = when {
item is Folder && selected -> selection + item
item is Folder && !selected -> selection - item
item is File && selected -> selection + item
item is File && !selected -> selection - item
else -> throw Exception()
}
}
更直接的版本是:
fun changeSelection(item: FileOrFolder, selected: Boolean) {
selection = if(selected)
when(item) {
is Folder -> selection + item
is File -> selection + item
else -> throw Exception()
}
else
when(item) {
is Folder -> selection - item
is File -> selection - item
else -> throw Exception()
}
}
但我绝对更喜欢:
operator fun Selection.plus(f: FileOrFolder) = ...
operator fun Selection.minus(f: FileOrFolder) = ...
fun changeSelection(item: FileOrFolder, selected: Boolean) {
if(selected)
selection += item
else
selection -= item
}