What is the Nothing type in Kotlin, and what does it mean for a function to be declared with return type Nothing?
answer
- Nothing = bottom type, no instances
- Subtype of EVERY type
- Never returns normally: throw/loop/TODO
- Powers `?: throw` and unreachable-code analysis
- Nothing? = type of null
basics
~10 sNothing is the type for code that never returns normally — it always throws, loops forever, or stops the program. A function returning Nothing tells the compiler the line after it is unreachable.
solid answer
~40 s`Nothing` is Kotlin's bottom type: it has **no instances**, and it is a **subtype of every other type**. A function declared `: Nothing` cannot return normally — it must throw, loop forever, or otherwise never complete. The standard library uses it for `TODO()`, `error()`, and `throw` (which itself is an expression of type Nothing). Because Nothing is a subtype of everything, an expression that produces Nothing can appear anywhere any type is expected, which powers the Elvis operator `?: throw ...` and `val x = node ?: return`. The compiler also uses a Nothing return to mark following code as **unreachable** (dead code) and to drive **definite-return / smart-cast** analysis: after `requireNotNull(x)` (returns the value, but throws on null) or after a `Nothing`-returning branch, the compiler narrows types accordingly.
code
kotlin · 4 linesfun fail(msg: String): Nothing = throw IllegalStateException(msg)
fun lookup(map: Map<String, Int>, key: String): Int =
map[key] ?: fail("missing $key") // Nothing <: Int makes this type-checkgo deeper
Recognizes Nothing marks code that always throws or loops and never returns.
States Nothing is the bottom type with no instances, subtype of everything, and lists TODO/error/throw.
Explains how Nothing drives unreachable-code and smart-cast analysis and powers ?: flow.
Positions Nothing as the formal bottom type completing the lattice with Any? as top, and its role in type inference soundness.
## What Nothing is `Nothing` is the **bottom type** of Kotlin's type hierarchy: - It has **zero instances** — you can never create a `Nothing` value. - It is a **subtype of every type** (the dual of `Any?`, the top type). So a `Nothing` expression is assignable to anything. A function whose return type is `Nothing` **never returns normally**. The only ways to "finish" are to throw, loop forever, or call another Nothing function. ```kotlin fun fail(msg: String): Nothing { throw IllegalStateException(msg) } fun loopForever(): Nothing { while (true) { /* ... */ } } ``` ## Why it is useful: control flow Because Nothing is a subtype of everything, an expression of type Nothing fits where any type is expected. That is exactly what lets you put a throw on the right side of Elvis: ```kotlin val name: String = user.name ?: fail("no name") // fail() returns Nothing <: String, so the whole expression is String ``` The same trick works with `throw` (which is itself of type `Nothing`) and with `return`/`break`/`continue`. ## Unreachable-code analysis After a call that returns Nothing, the compiler knows control cannot continue, so it marks the following statements as **unreachable**: ```kotlin fun handle(x: Int): String { if (x < 0) fail("negative") // returns Nothing return "ok $x" // reachable only when x >= 0 } ``` ## Standard-library Nothing functions - `TODO(): Nothing` — throws `NotImplementedError`; lets a stubbed function still type-check as any return type. - `error(message): Nothing` — throws `IllegalStateException`. - `throw e` — the throw expression itself has type `Nothing`. - `require(...)`/`check(...)` return Unit but their failing path is Nothing-typed; `requireNotNull`/`checkNotNull` return the non-null value. ## Nothing? — the null-only type `Nothing?` is the type of the literal `null` and of nothing else (its only value is null). Useful in generics and as the inferred type of `null` before smart resolution. ## Key terms - **Bottom type**: a type that is a subtype of all types and has no values. - **Unreachable / dead code**: statements the compiler proves can never execute. - **Definite assignment / smart cast**: type narrowing the compiler performs after a Nothing branch.
- Why is Nothing a subtype of every type, and how does that help the Elvis operator?As the bottom type it fits any expected type, so `a ?: throw ...` or `a ?: fail()` type-checks because the throw branch (Nothing) is a subtype of a's type.
- What is the difference between Nothing and Nothing??Nothing has no values at all; Nothing? has exactly one value, null — it is the type of the null literal.
A Nothing function is a one-way trapdoor: once you step on it, you never come back to the next line.
saying these in an interview costs you the question
- Saying Nothing is just another name for Unit
- Claiming you can create a Nothing instance
- Not knowing Nothing is a subtype of all types
- Thinking a Nothing function may sometimes return normally
- Unaware that `throw` is itself an expression of type Nothing