Combination of "else" and other branches in "when" expression

Hi. in some cases, one branch and “else” branch do the same work in “when” expression. for example:

when (x) {
    1 -> doJobOne()
    2 -> doJobTwo()
    else -> {
      doJobOne()
    }
}

I tried to do the below code but I faced an error:

when (x) {
    2 -> doJobTwo()
    1, else -> {
      doJobOne()
    }
}
1 Like

in that case just a plain else is enough because it’ll also match the 1 automatically. I.e the 2nd example is just equivalent to this:

when (x) {
    2 -> doJobTwo()
    else -> {
      doJobOne()
    }
}