Kotlin‘ when’语句 vs Java‘ switch’

Kotlin 的模式匹配很好,而且90% 的用例中它不执行下一个模式匹配的情况都很好。

在 Android 中,当数据库更新时,如果我们没有中断代码,我们使用 Java switch 属性继续下一个例子:

switch (oldVersion) {
case 1: upgradeFromV1();
case 2: upgradeFromV2();
case 3: upgradeFromV3();
}

因此,如果有人有一个应用程序与版本1的数据库和错过了应用程序版本与数据库 v2,他将得到所有需要的升级代码执行。

转换到 Kotlin,我们得到了这样的混乱:

when (oldVersion) {
1 -> {
upgradeFromV1()
upgradeFromV2()
upgradeFromV3()
}
2 -> {
upgradeFromV2()
upgradeFromV3()
}
3 -> {
upgradeFromV3()
}
}

这里我们只有3个版本,想象一下当 DB 达到19版本。

反正做的时候都是一样的,然后就换了? 我试着继续不走运的做下去。

43730 次浏览

简单但冗长的解决方案是:

if (oldVersion <= 1) upgradeFromV1()
if (oldVersion <= 2) upgradeFromV2()
if (oldVersion <= 3) upgradeFromV3()

函数引用的另一种可能的解决方案是:

fun upgradeFromV0() {}
fun upgradeFromV1() {}
fun upgradeFromV2() {}
fun upgradeFromV3() {}


val upgrades = arrayOf(::upgradeFromV0, ::upgradeFromV1, ::upgradeFromV2, ::upgradeFromV3)


fun upgradeFrom(oldVersion: Int) {
for (i in oldVersion..upgrades.lastIndex) {
upgrades[i]()
}
}

这样吧:

fun upgradeFromV3() {/* some code */}
fun upgradeFromV2() {/* some code */ upgradeFromV3()}
fun upgradeFromV1() {/* some code */ upgradeFromV2()}
fun upgradeFromV0() {/* some code */ upgradeFromV1()}


fun upgrade(oldVersion: Int) {
when (oldVersion) {
1 -> upgradeFromV1()
2 -> upgradeFromV2()
3 -> upgradeFromV3()
}
}

补充说明:

我喜欢 @ lukle将升级路径定义为一个列表的想法。这允许为不同的初始阶段定义不同的升级路径。例如:

  1. 从发布版本到最新发布版本的简单快速路径
  2. 追赶路径从热修复版本(可能很少在一行) ,这不应该应用时,从上一个完整版本到下一个完整版本

为此,我们需要知道应用列表中的哪些元素。

fun <Vs, V> Pair<Vs, V>.apply(upgrade: () -> Unit): (V) -> V {
return { current: V ->
if (first == current) {
upgrade()
second
} else {
current
}
}
}


val upgradePath = listOf(
(0 to 10).apply  { /* do something */ },
(5 to 15).apply  { /* do something */ },
(10 to 20).apply { /* do something */ },
(15 to 20).apply { /* do something */ },
(20 to 30).apply { /* do something */ },
(30 to 40).apply { /* do something */ }
)


fun upgrade(oldVersion: Int) {
var current = oldVersion
upgradePath.forEach { current = it(current) }
}

在此代码中,V 可以与 V 相同,也可以是使用重写的 equals(other: Any?): Boolean方法的某种 V 值集合。

编辑: 原始回复如下。以下是我目前正在做的:

fun upgrade() {
fun upgradeFromV1() { /* Do stuff */ }
fun upgradeFromV3() { /* Do stuff */ }


tailrec fun upgradeFrom(version: Int): Unit = when (version) {
LATEST_VERSION -> {
Config.version = version
} 1 -> {
upgradeFromV1()
upgradeFrom(2)
} in 2..3 -> {
upgradeFromV3()
upgradeFrom(4)
} else -> {
Log("Uncaught upgrade from $version")
upgradeFrom(version+1)
}


upgradeFrom(Config.version)
}

下面是 C. A.B. 给出的答案的一个变体:

fun upgrade(oldVersion: Int) {
when (oldVersion) {
latestVersion -> return
1 -> upgradeFromV1()
2 -> upgradeFromV2()
3 -> upgradeFromV3()
}
upgrade(oldVersion + 1)
}

下面是来自 bashor 的两个答案,加上一点功能性糖:

fun upgradeFromV0() {}
fun upgradeFromV1() {}
fun upgradeFromV2() {}
fun upgradeFromV3() {}


val upgrades = arrayOf(::upgradeFromV0, ::upgradeFromV1, ::upgradeFromV2, ::upgradeFromV3)


fun upgradeFrom(oldVersion: Int) {
upgrades.filterIndexed { index, kFunction0 -> oldVersion <= index }
.forEach { it() }
}

绝对有可能 引自官方参考文献: 控制流: if,when,for,while

If many cases should be handled in the same way, the branch conditions may be combined with a comma:


when (x) {
0, 1 -> print("x == 0 or x == 1")
else -> print("otherwise")
}

因此,如果相同的条件列表很短,那么您可以按昏迷分隔列出它们,或者使用类似条件的范围1。.10如其他答案所述

OP 的另一种回答:

override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
when (oldVersion) {
newVersion -> return
1 -> TODO("upgrade from v1 to v2")
2 -> TODO("upgrade from v2 to v3")
}
oldVersion++
onUpgrade(db, oldVersion, newVersion)
}
val orders = arrayListOf(
{ upgradeFromV1()},
{ upgradeFromV2()},
{ upgradeFromV3()}
)


orders.drop(oldVersion).forEach { it() }

Kotlin 采用了一种不同的流量控制方式,称为 when。

你的代码,使用它的时候,可以是这样的。

显然,代码可能不同,但我明白你的问题只是关于开关的使用。

fun main(args: Array<String>) {
val month = 8


val monthString = when(month) {
1 -> "Janeiro"
2 -> "February"
3 -> "March"
4 -> "April"
5 -> "May"
6 -> "June"
7 -> "July"
8 -> "August"
9 -> "September"
12 -> "October"
11 -> "November"
10 -> "December"
else -> "Invalid month"
}


println(monthString);
}

用于自定义实现的 Kotlin DSL 怎么样:

class SwitchTest {


@Test
fun switchTest() {


switch {
case(true) {
println("case 1")
}
case(true) {
println("case 2")
}
case(false) {
println("case 3")
}
caseBreak(true) {
println("case 4")
}
case(true) {
println("case 5")
}
//          default { //TODO implement
//
//          }
}
}
}


class Switch {
private var wasBroken: Boolean = false


fun case(condition: Boolean = false, block: () -> Unit) {
if (wasBroken) return
if (condition)
block()
}


fun caseBreak(condition: Boolean = false, block: () -> Unit) {
if (condition) {
block()
wasBroken = true
}
}
}


fun switch(block: Switch.() -> Unit): Switch {
val switch = Switch()
switch.block()
return switch
}

它打印: 个案1 个案2 个案4 UPD: 一些重构和输出示例。

只需使用 for 循环和 when。

for (version in oldVersion..newVersion) when (version) {
1 -> upgradeFromV1()
2 -> upgradeFromV2()
3 -> upgradeFromV3()
}

如果你不关心这些函数的运行顺序,你可以自己做一个伪切换,比如:

function PretendSwitch() {
if(oldVersion>3) return
upgradeFromV3();
if(oldVersion==3) return
upgradeFromV2()
if(oldVersion==2) return
upgradeFromV1()
if(oldVersion==1) return
}

没有什么比用开关更干净了。不幸的是,Kotlin 缺少 switch 语句,因此无法优雅地执行此操作。

Val oldVersion = 6 Val newVersion = 10

for (version in oldVersion until newVersion) {
when (version) {
1 -> upgradeFromV1()
2 -> upgradeFromV2()
3 -> upgradeFromV3()
4 -> upgradeFromV4()
5 -> upgradeFromV5()
6 -> upgradeFromV6()
7 -> upgradeFromV7()
8 -> upgradeFromV8()
9 -> upgradeFromV9()
}
println("~~~")
}