What is the purpose of an empty class in Kotlin?

I went through a Kotlin background document and then saw this.

A class declaration consists of a class name, a class title (indicating its type parameters, the main constructor, etc.) and a class surrounded by curly braces. Both the title and the body are optional; if the class does not have a body, curly braces may be omitted.

class Empty

Now I'm wondering what a class declaration is without a title and a body

+6
source share
5 answers

TL; DR: they want to demonstrate it is possible

Any . , , .

Java- :

public final class Empty {
}
+3

, . .

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

, java enum .

+5

. , .

, , , , (, , empty type uninhabited type).

Kotlin Nothing, ( , ) https://github.com/JetBrains/kotlin/blob/master/core/builtins/native/kotlin/Nothing.kt

Nothing. , ( Unit, Unit). assertFail, , . - , , (Nothing).

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

, Function<*, String> Function<in Nothing, String>

:

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

, : ++

+3

. - , . , . - :

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

class Address

, , , - , , , .

+1

Spring Boot:

@SpringBootApplication
class FooApplication

fun main(args: Array<String>) {
    runApplication<FooApplication>(*args)
}
+1
source

All Articles