Nothing
What is Nothing in Kotlin? Does it even exist? A Nothing return type indicates a function that never returns. This is usually a function that always throws an exception.
Here is an example.
fun infinity(): Nothing {
while (true) {}
}
As you can see, we are planning to loop forever. Hence it will only make sense if we tell the caller that we shall return Nothing. Nothing is a built-in Kotlin type with no instances. In our codebase, there is one function we do it very often. Yes, TODO() which has a return type of Nothing and throws NotImplementedError when it is invoked.
fun willDoLater() = TODO()
fun willDoLaterWithCustomMsg = TODO("later, ok?")
willDoLater() // will return "NotImplementedError: An operation is not implemented."
willDoLaterWithCustomMsg() // will return "NotImplementedError: An operation is not implemented: later, ok?"
Cousins of Nothing
We could also assign both null or none to a var or val of a nullable type. This is allowed because the type of both null and none is Nothing? (nullable Nothing).
val a: Nothing? = null
var b: String? = null
b = "abc"
b = none
print(b) // null
