How to convert function output to Unit with Kotlin

前端 未结 6 463
轻奢々
轻奢々 2021-01-12 11:48

I have troubles with a function in Kotlin that should return Unit, but due to a usage of another function returning a Boolean, there is a type mismatch.

Here is a co

6条回答
  •  抹茶落季
    2021-01-12 12:27

    I think the last is most idiomatic. Though you don't need explicit : Unit, it's the default for block form if no return type is specified and if you try to return something else you'll get an error.

    But note a subtle detail: when when is used as an expression, else is required unless the compiler can prove all cases are handled; in the block form it's "used as a statement" and unhandled cases are ignored.

提交回复
热议问题