8d25c20169
Sealed classes can be derived only by their own inner classes or objects. Their constructors cannot be called explicitly, so compiler knows all their descendants. Incompatible modifier checks (final, abstract). Impossible with interface, object, enum. A pack of tests provided.
14 lines
458 B
Kotlin
Vendored
14 lines
458 B
Kotlin
Vendored
sealed class Sealed {
|
|
object First: Sealed()
|
|
open class NonFirst: Sealed() {
|
|
object Second: NonFirst()
|
|
object Third: NonFirst()
|
|
fun foo(): Int {
|
|
val s = object: <!SEALED_SUPERTYPE_IN_LOCAL_CLASS!>Sealed<!>() {}
|
|
class Local: <!SEALED_SUPERTYPE_IN_LOCAL_CLASS!>Sealed<!>() {}
|
|
return s.hashCode()
|
|
}
|
|
}
|
|
val p: Sealed = object: <!SEALED_SUPERTYPE_IN_LOCAL_CLASS!>Sealed<!>() {}
|
|
}
|