Nothing & Any Quiz

KOTLIN › Types & Classes

What is Nothing in Kotlin's type system?

Answer: The bottom type: a subtype of every other type, with no instances

Nothing is the bottom type. It sits below every type in the hierarchy and can never be instantiated. Functions typed Nothing never return normally.

Why does this compile without a type error?

Answer: throw is an expression of type Nothing, a subtype of String

throw is an expression typed Nothing. Since Nothing is a subtype of every type, it unifies with String and the overall expression types as String.

What is the type of x here?

Answer: Nothing?

Without an explicit type, the compiler infers the type of a bare null literal as Nothing? -- the type whose only value is null.

What is the return type of TODO()?

Answer: Nothing

TODO() always throws NotImplementedError, so it never returns. Its return type is Nothing, which lets it stand in for any return type.

Which statement about Any is correct?

Answer: Any is the supertype of all non-nullable types; Any? covers nullable ones too

Any is the root of non-nullable types. Any? is the true top of the hierarchy, encompassing both nullable and non-nullable types. At the JVM level Any maps to Object, but in Kotlin source they are distinct.

Why can emptyList() be assigned to List<String>?

Answer: emptyList() is backed by a List<Nothing> singleton, and Nothing is a subtype of String

emptyList() is fun <T> emptyList(): List<T> -- the compiler infers T from context. Its implementation returns a singleton typed List<Nothing>. Because List is covariant (out T) and Nothing <: String, that singleton is assignable to List<String>.

What is the difference between Unit and Nothing?

Answer: Unit has one instance and means "returns but no useful value"; Nothing has zero instances and means "never returns"

Unit has exactly one value (the Unit object) and signals a function returns but its return value is meaningless. Nothing has no instances and signals a function never completes normally.

What does this function return for flag = false?

Answer: It throws NotImplementedError

TODO() has type Nothing, so the compiler never reaches it when flag is false. When flag is true the throw fires. When false, the function falls through with no explicit return, which in Kotlin is an implicit return Unit.

Which of these is NOT a function that returns Nothing?

Answer: println("done")

TODO(), error(), and a bare throw all have type Nothing because they never return. println() returns Unit -- it completes normally.

What does Any declare (its own members)?

Answer: equals(), hashCode(), toString()

Kotlin's Any declares exactly three members: equals(), hashCode(), and toString(). Java's Object has more (like clone(), wait(), notify()), but those are not part of Any's Kotlin API.

What happens at run time?

Answer: Prints null because null is a valid Any?

null has type Nothing?, which is a subtype of Any?. Adding it to a MutableList<Any?> is perfectly legal, and printing it shows null.

Why does the compiler require an else branch here?

Answer: Any is open, so the compiler cannot know all subtypes at compile time

Any is open -- any class can extend it, so the compiler cannot enumerate all possible subtypes. Only sealed types provide exhaustive when without else.

What is the inferred type of result?

Answer: String

The when is exhaustive (all branches of the sealed class are covered). The Success branch returns String. The Error branch calls error(), which returns Nothing. The union of String and Nothing is String, because Nothing is a subtype of String.

Can you write a function with return type Nothing that does NOT throw?

Answer: Yes, with an infinite loop

A function that loops forever never returns, so its return type is Nothing. It does not throw -- it simply never completes. Nothing means "no normal return", not "must throw".

Back to Nothing & Any