首页 > 解决方案 > 什么时候是详尽的,所以其他是多余的?

问题描述

我是 kotlin 的新手,我创建了一个包含 when 语句和 IntelliJ 建议我删除 else 分支的方法。我不太确定为什么。知道为什么我需要在这里删除 else 分支吗?这是代码:

    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")
        }
}

标签: kotlinappium

解决方案


当您用尽所有可能的选项时,when没有理由拥有 else 分支。这具有额外的优势,即在将元素添加到枚举而不扩展when.


推荐阅读