kotlin - 什么时候是详尽的,所以其他是多余的?
问题描述
我是 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")
}
}
解决方案
当您用尽所有可能的选项时,when
没有理由拥有 else 分支。这具有额外的优势,即在将元素添加到枚举而不扩展when
.
推荐阅读
- bash - 使用“on-the-fly”参数执行 PGP 加密的 shell 脚本
- drake - 如何在没有模拟的情况下可视化新姿势,使用 VTKRenderer
- arrays - PostgreSQL:在 WITH 构造中使用列作为数组
- html - CSS自动填充顶部和底部以始终覆盖高度
- php - 以编程方式在购物车中添加 WC Booking 失败
- javascript - 如何从 aReadStream 文本文件中删除 \n 和 \r
- hive - Flink通过流模式读取Hive,为什么只读取最新的分区,即使我将开始偏移设置为最旧的分区?
- python - 熊猫:如果列符合条件,则添加行
- sql - postgres 文本字段到具有空值和空值的数字
- node.js - 将 Ember.js 应用程序部署到 Heroku 时如何解决“无法下载节点 14.18.0”?