Feature Request: data objects and sealed interfaces

Specifically, instead of doing this:

sealed class Event {
    data class EventWithName(val name:String):Event()
    data class EventWithNumber(val number:Int):Event()
    object EventWithoutArgs:Event(){
        override fun toString(): String = javaClass.simpleName
    }
}
fun main() {
    println(Event.EventWithName("hello"))
    println(Event.EventWithNumber(123))
    println(Event.EventWithoutArgs)
}

I would like to be able to do this:

sealed interface Event {
    data class EventWithName(val name:String):Event
    data class EventWithNumber(val number:Int):Event
    data object EventWithoutArgs:Event
}
fun main() {
    println(Event.EventWithName("hello"))
    println(Event.EventWithNumber(123))
    println(Event.EventWithoutArgs)
}

And still have a string like “EventWithoutArgs” instead of something like “Event$EventWithoutArgs@4edde6e5”

The code just seems simpler and more intuitive right?

2 Likes

Thank you for suggestion.
Regarding sealed interfaces please watch and vote for https://youtrack.jetbrains.com/issue/KT-20423
Regarding object’s toString() please follow https://youtrack.jetbrains.com/issue/KT-4107
There data objects are suggested among other thoughts in the discussion.