Sealed Classes

    To declare a sealed class, you put the modifier before the name of the class. A sealed class can have subclasses, but all of them must be declared in the same file as the sealed class itself. (Before Kotlin 1.1, the rules were even more strict: classes had to be nested inside the declaration of the sealed class).

    A sealed class is abstract by itself, it cannot be instantiated directly and can have abstract members.

    Note that classes which extend subclasses of a sealed class (indirect inheritors) can be placed anywhere, not necessarily in the same file.

    1. fun eval(expr: Expr): Double = when(expr) {
    2. // the `else` clause is not required because we've covered all the cases