Pattern matching in Kotlin is nice and the fact it does not execute next pattern match is good in 90% of use cases.
In Android, when database is updated, we use Java switch property to go on next case if we do not put a break to have code looking like that:
switch (oldVersion) {
case 1: upgradeFromV1();
case 2: upgradeFromV2();
case 3: upgradeFromV3();
}
So if someone has an app with version 1 of the DB and missed the app version with DB v2, he will get all the needed upgrade code executed.
Converted to Kotlin, we get a mess like:
when (oldVersion) {
1 -> {
upgradeFromV1()
upgradeFromV2()
upgradeFromV3()
}
2 -> {
upgradeFromV2()
upgradeFromV3()
}
3 -> {
upgradeFromV3()
}
}
Here we have only 3 version, imagine when DB reaches version 19 :/
Anyway to makes when acting in the same way than switch? I tried continue without luck.
edit: Original response below. Here's what I'm currently doing:
Here's a variation on the answer @C.A.B. gave:
Here is a mix of the two answers from bashor, with a little bit of functional sugar:
Another variation of OP's answer:
It is absolutly possible quote from official reference : Control Flow: if, when, for, while
So if same condition list is short, then you can list them separating by coma, or use ranges like condition in 1..10 as stated in other answers
Simple but wordy solution is:
Another possible solution with function references: