0

使用密封类时,编译器仅检查同一文件中的子类型。

密封类的一大卖点是使用when表达式时的详尽检查。那么为什么不实施呢?

在 1 个文件中,我有:

class C : B()

在另一个文件中,我有:

sealed class A

open class B : A()

fun switch(input: A) =
    when(input) {
        is B -> Unit
//        is C -> Unit - I expect a compiler error since this is a subtype and it's commented out
    }
4

1 回答 1

0

正如 Eugene Petrenko评论is B那样,编译器所需的案例涵盖了 B 的所有子类。

于 2019-01-24T00:53:59.740 回答