Pattern matching in Kotlin is nice and the fact it does not execute the next pattern match is good in 90% of use cases.
在Android中,当数据库更新时,如果我们不中断代码如下所示,我们将使用Java Switch属性继续下一种情况:
switch (oldVersion) {
case 1: upgradeFromV1();
case 2: upgradeFromV2();
case 3: upgradeFromV3();
}
因此,如果某人有一个DB版本为1的应用程序,但错过了DB v2的应用程序版本,他将执行所有需要的升级代码.
改用Kotlin 后,我们会看到一团糟,比如:
when (oldVersion) {
1 -> {
upgradeFromV1()
upgradeFromV2()
upgradeFromV3()
}
2 -> {
upgradeFromV2()
upgradeFromV3()
}
3 -> {
upgradeFromV3()
}
}
这里我们只有3个版本,想象一下当DB达到版本19时.
Anyway to makes when acting in the same way then switch? I tried to continue without luck.