What is the purpose of empty class in Kotlin?

后端 未结 6 1609
無奈伤痛
無奈伤痛 2021-01-04 21:08

I was going through Kotlin reference document and then I saw this.

The class declaration consists of the class name, the class header (

6条回答
  •  伪装坚强ぢ
    2021-01-04 21:40

    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.

提交回复
热议问题