标签: kotlin-when

避免 kotlin 中“When”中的 else 条件

根据When in Kotlin 的文档,如果编译器知道所有值都被覆盖,则 else 不是强制性的。对于 emum 或密封类来说这是非常重要的,但是对于数字 1 到 5 的数组(startRating)如何做到这一点。

private fun starMapping(startRating: Int): String {

    return when (startRating) {
        1 -> "Perfect"
        2 -> "Great"
        3-> "Okay"
        4-> "Bad"
        5-> "Terrible"
        // don't want to add else as I believe it is prone to errors.
    }
}
Run Code Online (Sandbox Code Playgroud)

与此类似的东西

return when (AutoCompleteRowType.values()[viewType]) {
        AutoCompleteRowType.ITEM -> ItemView(
                LayoutInflater.from(parent.context).inflate(R.layout.item_venue_autocomplete_item_info, parent, false))

        AutoCompleteRowType.SECTION -> SectionView(
                LayoutInflater.from(parent.context).inflate(R.layout.item_venue_autocomplete_section, parent, false)
        )
    }
Run Code Online (Sandbox Code Playgroud)

kotlin kotlin-when

9
推荐指数
1
解决办法
9159
查看次数

如何修复 Kotlin 中的“密封类/接口上的非详尽‘when’语句”

Kotlin 1.7when中将禁止对密封类/接口进行非详尽的声明。

我有一个sealed class State和它的孩子:

sealed class State {
    object Initializing : State()
    object Connecting : State()
    object Disconnecting : State()
    object FailedToConnect : State()
    object Disconnected : State()
    object Ready : State()
}
Run Code Online (Sandbox Code Playgroud)

在某些情况下,我只想处理特定的项目,而不是全部,例如:

val state: State = ... // initialize
when (state) {
    State.Ready -> { ... }
    State.Disconnected -> { ... }
}
Run Code Online (Sandbox Code Playgroud)

但我收到一条警告(在Kotlin 1.7中我猜这将是一个错误),说:

1.7 中将禁止在密封类/接口上使用非详尽的“when”语句,而是添加“Connecting”、“Disconnecting”、“FailedToConnect”、“Initializing”分支或“else”分支

else -> {}像下面的代码一样在这里使用空分支是一个好习惯吗?

when (state) {
    State.Ready -> { ... }
    State.Disconnected -> …
Run Code Online (Sandbox Code Playgroud)

android kotlin sealed-class kotlin-when kotlin-sealed

9
推荐指数
1
解决办法
1万
查看次数

阻止时在Kotlin中调用String方法

目前,我有一个when块,像这样:

String foo = getStringFromBar()

when {
    foo == "SOMETHING" -> { /*do stuff*/ }
    foo == "SOMETHING ELSE" -> { /*do other stuff*/ }
    foo.contains("SUBSTRING") -> { /*do other other stuff*/ }
    else -> { /*do last resort stuff*/ }
}
Run Code Online (Sandbox Code Playgroud)

有什么办法可以简化为这样的事情:

String foo = getStringFromBar()

when (foo) {
    "SOMETHING" -> { /*do stuff*/ }
    "SOMETHING ELSE" -> { /*do other stuff*/ }
    .contains("SUBSTRING") -> { /*do other other stuff*/ }  // This does not work
    else -> { …
Run Code Online (Sandbox Code Playgroud)

syntax android kotlin kotlin-when

4
推荐指数
1
解决办法
80
查看次数

Kotlin:当枚举名称明确时省略枚举名称

使用Swift 枚举,您可以省略名称enum,在只能使用该类型的值的情况下,。

所以当给出枚举(Swift/Kotlin)

enum (class) CompassPoint {
  case north
  case south
  case east
  case west
}
Run Code Online (Sandbox Code Playgroud)

Swift 在创建新变量时只需要枚举名称:

// type unclear, enum name needed
var directionToHead = CompassPoint.west

// type clear, enum name can be dropped
directionToHead = .east

// type clear, enum name can be dropped
switch directionToHead {
case .north:
  print("Lots of planets have a north")
case .south:
  print("Watch out for penguins")
case .east:
  print("Where the sun rises")
case .west:
  print("Where the skies are …
Run Code Online (Sandbox Code Playgroud)

enums kotlin kotlin-when

2
推荐指数
1
解决办法
284
查看次数