使用sealed classes时,编译器仅检查同一文件中的子类型。
密封类的一大卖点是在使用when表达式时进行详尽的检查。那么,为什么没有实现它呢?
在一个文件中,我有:
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
}发布于 2019-01-24 08:54:00
作为Eugene Petrenko commented,编译器所需的is B用例涵盖了B的所有子类。
https://stackoverflow.com/questions/54334380
复制相似问题