What is the purpose of empty class in Kotlin?

后端 未结 6 1610
無奈伤痛
無奈伤痛 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:20

    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 {
    }
    
    0 讨论(0)
  • 2021-01-04 21:24

    From practical programmer day to day perspective empty class makes no much sense indeed. There are however cases where this behavior is desirable.

    There are scenarios where we want to make sure that we want to define a class and at the same time, we want to make sure that instance of this class will never be created (type created from such class is called empty type or uninhabited type).

    Perfect example of this is Kotlin Nothing class with do not have class declaration header and body (notice that it also have private constructor) https://github.com/JetBrains/kotlin/blob/master/core/builtins/native/kotlin/Nothing.kt

    There are few usages for Nothing in Kotlin language. One of them would be a function that does not return a value (do not confuse this with Unit where the function returns actually returns a value of type Unit). A typical example is an assertFail method used for testing or method that exits current process. Both methods will never actually return any value yet we need to explicitly say tell it to a compiler using special type (Nothing).

    fun assertFail():Nothing {
       throw Exception()
    }
    

    Nothing can be also used with start projections where type Function<*, String> can be in-projected to Function<in Nothing, String>

    Another usage for empty class is type token or placeholder:

    class DatabaseColumnName
    class DatabaseTableName
    addItem(DatabaseColumnName.javaClass, "Age")
    addItem(DatabaseTableName.javaClass, "Person")
    ...
    getItemsByType(DatabaseTableName.javaClass)
    

    Some languages are using empty classes for metaprogramming although I haven't explored this part personally: Advantages of an empty class in C++

    0 讨论(0)
  • 2021-01-04 21:26

    It doesn't make much sense as a final result. However it can be useful in active development and at a design time as a placeholder of some sort, which may be expanded in the future. Such terse syntax allows you to quickly define such new types as needed. Something like:

    class Person (
        val FirstName: String,
        val LastName: String,
        // TODO
        val Address: Address
    )
    
    class Address
    

    I think main reason this is specifically mentioned in documentation is to demonstrate, that language syntax in general can be terse, not that it is specifically created for common usage.

    0 讨论(0)
  • 2021-01-04 21:30

    An example of empty class usage from Spring Boot framework:

    @SpringBootApplication
    class FooApplication
    
    fun main(args: Array<String>) {
        runApplication<FooApplication>(*args)
    }
    
    0 讨论(0)
  • 2021-01-04 21:33

    Sealed classes, in a sense, an extension of enum classes: the set of values for an enum type is also restricted, but each enum constant exists only as a single instance, whereas a subclass of a sealed class can have multiple instances which can contain state. reference

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题