Why Unit, instead of null?

When a function doesn’t return a useful value, the compiler assigns a return type of Unit. This is a special type that has exactly one value (confusingly also called Unit).

However, there’s another predefined type with exactly one value: Nothing?, with the value null.

Why was that not chosen for such functions instead?

It would probably be more intuitive to receive the value null (judging from the questions I see asking what this Unit is all about), and it would avoid an extra predefined type. (Unit brings other redundancies, too: for example, not only is Unit isomorphic to Nothing?, but Unit? is a type with two possible values, isomorphic to Boolean.)

I’m guessing one of the reasons is historical: functional languages generally have such a type, though I think it’s only called ‘unit’ in ML descendants (OCaml, Standard ML, F#), and Scala. However many others call it ‘null’ (or ‘nil’): Common Lisp, PHP, JavaScript, Ruby — so there’s precedent both ways.

Another possible reason could be complications from Nothing? being a subtype of all nullable types — but I can’t think of any.

Did Kotlin simply take it from Scala, without considering that Kotlin’s expressive type system already provided an alternative? Or is there some benefit to having both?

null and Unit are different things, and meant to be different. null expresses the absence of a value that would usually be there.

fun findByName(name: String): User?

Whereas Unit means essentially “no value”, not even null.

fun delete(item: Item): Unit

There is no useful return value in this case, what is expressed with Unit.

Nothing sounds similar but is the “subtype” of all types, or “bottom type” and is only used in the case of methods that could only be quit with an exception, as far as I know. So, it doesn’t express “missing return value” like null or “never a useful return value” like Unit, but “no normal return”. The TODO function can be used as the implementation of any function, because its return value, Nothing, is the subtype of any other type. That would be problematic as a backdoor in the type systems, if it wouldn’t lead to an exception in any case.

Partalli true. Nothing is the type of every expression that doesn’t return a value in the standard way. For example return, continue, break, throw and while(true) are all of type Nothing.