What is the purpose of empty class in Kotlin?

Empty classes can be useful to represent state along with other classes, especially when part of a sealed class. Eg.

sealed class MyState {
    class Empty : MyState()
    class Loading : MyState()
    data class Content(content: String) : MyState()
    data class Error(error: Throwable) : MyState()
}

In this way you can think of them like java enum entries with more flexibility.


tldr: they want to demonstrate it's possible

even an empty class is of type Any and therefore has certain methods automatically. I think in most cases, this does not make sense, but in the documentation case it's used to show the simplest possible definition of a class.

The Java equivalent would be:

public final class Empty {
}

Tags:

Oop

Kotlin