Chills
03/19/2020, 8:44 AMMichael de Kaste
03/19/2020, 8:52 AMChills
03/19/2020, 8:52 AMMichael de Kaste
03/19/2020, 8:55 AMcomplexity
as far as I know. They are just a combination of pointers to their fields.
sealed class is just a class with the added bonus that the compiler can know it's subclasses.Michael de Kaste
03/19/2020, 8:56 AMChills
03/19/2020, 8:59 AMChills
03/19/2020, 8:59 AMribesg
03/19/2020, 9:00 AMChills
03/19/2020, 9:03 AMribesg
03/19/2020, 9:05 AMChills
03/19/2020, 9:06 AMMichael de Kaste
03/19/2020, 9:08 AMMichael de Kaste
03/19/2020, 9:17 AMsealed class Operation {
class Add(val value: Int) : Operation()
class Substract(val value: Int) : Operation()
class Multiply(val value: Int) : Operation()
class Divide(val value: Int) : Operation()
}
Now the compiler knows that Operation only has Add, Substract, Multiply and Divide.
You can make an execute function e.g.:
fun execute(x: Int, op: Operation) = when (op) {
...
Operation.Increment -> x + 1
Operation.Decrement -> x - 1
}
and you don't need to add an `else' statement and do something like: "Unknown subclass performing execute"Ruckus
03/19/2020, 2:12 PMelse
clause for you (at least for a when expression), thus it's a compile time feature.