也许这是个荒谬的问题。我有一个方法接收
Command
(密封类)和退货
Unit
我希望编译器崩溃
when
尚未实施分支:
sealed class Command
class FirstCommand : Command()
class SecondCommand: Command()
fun handle(cmd: Command) {
when(cmd) {
is FirstCommand -> //whatever
}
}
上面的代码没问题,但我不想编译它。
当方法返回与
单位
,它不编译:
fun handle(cmd: Command) : Any {
when(cmd) { //error, when must be exhaustive and requires all branches
is FirstCommand -> //whatever
}
}