Kotlin: Using objects in sealed classes to represent stateless types

Niel de Wet
1 min readApr 3, 2019

--

Sealed classes are useful for representing a limited set of types a value may have. Said another way, it is a restricted type hierarchy in which sub-types are only allowed if they are part of the closed set. Data classes are a natural fit as elements of the set of types, but what if you wish to represent a stateless concept?

An example may be a sealed class representing different ways in which an entity may be updated. One valid value is “no change”. This can be represented by an object with no state.

--

--

No responses yet