6

I am new to kotlin and I created a method that contains the when statement and IntelliJ suggesting me to remove the else branch. I am not really sure why. Any idea why I need to remove the else branch here? This is the code:

    companion object{
    @Synchronized fun getDriver(
        url: String,
        desiredCapabilities: DesiredCapabilities,
        mobilePlatform: MobilePlatform)
        : AppiumDriver<WebElement> =
        when(mobilePlatform){
            MobilePlatform.ANDROID -> AndroidDriver<WebElement>(URL(url), desiredCapabilities)
            MobilePlatform.IOS -> IOSDriver<WebElement>(URL(url), desiredCapabilities)
            else -> throw RuntimeException("Cannot get the driver")
        }
}
Michael
  • 41,989
  • 11
  • 82
  • 128
kidulf
  • 419
  • 1
  • 6
  • 17
  • 1
    If `MobilePlatform` is a enum and it doesn't have any values except `ANDROID` and `IOS`, you don't need `else` branch. – ardenit Feb 17 '20 at 10:12

3 Answers3

8

When you have exhausted all possible options of when there is no reason to have an else branch. This has the added advantage that you get a compiler error after adding elements to the enum without extending the when.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
Christoph Grimmer
  • 4,210
  • 4
  • 40
  • 64
4

In kotlin, when on a sealed class object doesn't require else if all possible inner cases are covered.

Sample sealed class:

sealed class A {
 object B: A()
 object C: A()
}

Let the above be a sealed class then any object of class A (lets say a), can be used inside a when exhaustively(not necessarily) while returning

return when(a) {
  is A.B -> return something
  is A.C -> return something
} // no need of else here as all cases are covered.

There is one catch here, if you just need to check for one condition, let's say is A.B you can write an else. Also, please note that, you need NOT write exhaustive conditions/else if it's just a statement.

Example below:

some code ...
when(a) {
      is A.B -> do some task
}
more code ...

Hope this helps !!

vizsatiz
  • 1,933
  • 1
  • 17
  • 36
  • 1
    Note that from [Kotlin 1.6](https://blog.jetbrains.com/kotlin/2021/11/kotlin-1-6-0-is-released/#:~:text=Sealed%20(exhaustive)%20when,your%20own%20functions.%C2%A0) non-exhaustive `when` *statement*s will emit a warning and from Kotlin 1.7 they will emit an error. See [the issue](https://youtrack.jetbrains.com/issue/KT-47709). – Mahozad Nov 21 '21 at 14:42
0

To utilize else block you can try something like:

    enum class PaymentStatus(val value: Int) {
     PAID(1),
     UNPAID(2) 
 }

    fun f(x: Int) {
    val foo = when (x) {
        PaymentStatus.PAID.value -> "PAID"
        PaymentStatus.UNPAID.value -> "UNPAID"

        else -> throw IllegalStateException()
    }
}

OR

create factory method create in the companion object of enum class:

enum class PaymentStatus(val value: Int) {
    PAID(1),
    UNPAID(2);

    companion object {
        fun create(x: Int): PaymentStatus {
            return when (x) {
                1 -> PAID
                2 -> UNPAID
                else -> throw IllegalStateException()
            }
        }
    }
}

fun f(x: Int) {
    val foo = when (PaymentStatus.create(x)) {
        PaymentStatus.PAID -> "PAID"
        PaymentStatus.UNPAID -> "UNPAID"
    }
}

Check here more details

Muzzamil
  • 2,823
  • 2
  • 11
  • 23