What is kotlin.Unit, and what does a Kotlin function return when you don't declare a return type?
answer
- One type, one value: the Unit singleton
- No return type => returns Unit
- Both `: Unit` and `return Unit` are implicit
- Unit is a real object, not a keyword
- Used by side-effecting functions
basics
~10 sUnit is Kotlin's type for functions that do work but give back no useful value. If you don't write a return type, the function returns Unit automatically.
solid answer
~30 skotlin.Unit is a built-in type with exactly one value, the object Unit. Functions whose only purpose is a side effect (printing, mutating state) return Unit. The return type is optional: `fun log(msg: String) {}` is identical to `fun log(msg: String): Unit { return Unit }`. The Unit value and the `return` are both inserted implicitly by the compiler, so you never write them. It is Kotlin's analogue of Java's `void`, but unlike `void`, Unit is a real type you can name, store in a variable, and use as a generic type argument such as `Function0<Unit>` or `LiveData<Unit>`.
go deeper
Knows Unit = the 'no useful value' return type and that omitting the return type implies it.
Explains that Unit is a singleton object with one value and that both the type and the return are implicit.
Connects Unit being a real type to its use in generic positions like () -> Unit and Function0<Unit>.
Frames Unit within the type lattice and contrasts a one-value type (carries no info) with void and with Nothing.
## What Unit is `Unit` is a built-in Kotlin type that has **exactly one value** -- the singleton `object Unit`. Because there is only one possible value, returning Unit carries **no information**: callers learn nothing from it except that the function finished. It is the type used by functions that exist for their **side effects** (printing, writing to a database, mutating a variable) rather than for producing a result. ## Implicit return type and implicit return If you omit the return type, Kotlin infers `Unit`: ```kotlin fun greet(name: String) { // return type is Unit println("Hi, $name") } // Fully explicit equivalent fun greet2(name: String): Unit { println("Hi, $name") return Unit } ``` Both the `: Unit` and the `return Unit` are inserted by the compiler. You almost never write them by hand. ## Unit is a real value Unlike a keyword, `Unit` is an ordinary object you can reference: ```kotlin val x: Unit = Unit // legal val y = greet("Ann") // y has type Unit, value Unit ``` This matters when Unit appears as a **generic type argument**, e.g. a `Function0<Unit>` (a lambda returning nothing useful), or `Result<Unit>`, or `Channel<Unit>` used purely for signalling. ## Key keywords/APIs - The type `Unit` and its single value `Unit`. - Implicit `return Unit` at the end of a Unit-returning function. - Generic positions like `() -> Unit`, `Function0<Unit>`.
- Can you store the result of a Unit-returning function in a variable?Yes. `val x = println("hi")` gives x type Unit and value the Unit singleton; it is legal but pointless.
- How many instances of Unit exist?Exactly one -- Unit is a singleton object, so all Unit values are the same instance.
Unit is like a delivery confirmation slip: it tells you the job is done but carries no other information.
saying these in an interview costs you the question
- Saying Unit means 'no return statement is allowed'
- Claiming Unit is a keyword like void rather than a type with a value
- Thinking you must explicitly write `return Unit`
- Saying Unit can have many values
- Confusing Unit with null