Get all free code and projects on java tutorials, framework,android game and tutorials
Nothing Type in Kotlin
A colleague approached me a while ago and asked me a question about Kotlin. What was this “Nothing type”, and what was it used for? I did not really have a good answer for him at the time.
Since then, I’ve found my answer.
In mathematics, bottom (often denoted as _|_) represents the absolute minimum value something can hold.
Nothing type has no instances. You can use Nothing to represent “a value that never exists”: for example, if a function has the return type of Nothing, it means that it never returns (always throws an exception).
When we’re talking about types, we can think of Nothing as being the absolute bottom (leaf) object of any type hierarchy.
What we can see in this diagram (figure 1) is that Nothing is the opposite of Any. While every object you will work with in Kotlin implicitly extends Any, so to does Nothing implicity extend any object that exists. This makes Nothing the ‘dual’ or opposite of Any.