I understand what Unit is, but why is it called “unit”? That name doesn’t make any intuitive sense. It would help to know what “unit” means in English in this context. Thanks.
The accepted answer here is from one of the designers of Kotlin.
The short version (from that link) is that it’s “a traditional name”.
I find this a shame. Kotlin hasn’t been afraid to replace traditional names where there are clearer alternatives (e.g. using fun
to define functions instead of the traditional def
, and replacing find()
with first()
).
And they’ve obviously taken great care to make Kotlin very easy to read — I find it clearer and more readable than almost any other language. (Which is doubly impressive considering that I suspect English isn’t the native language of many of the designers. As someone with only minimal knowledge of anything but English, I have huge respect for anyone who can become fluent in multiple languages; and IMO both Kotlin’s syntax and standard library read better and more naturally than most languages designed by native English speakers!)
But while the name ‘Unit’ may make good sense in the branch of mathematics from which it comes, and has been traditional in the functional programming world, it’s demonstrably less clear to the rest of us… The concept makes a lot of sense — but sadly, in this case I think the designers missed an opportunity by not finding a more intuitive name.
I think it’s a decent name for what it means in the context of Kotlin. A function with a return type of Int
returns some data that is of type Int
. A function with a return type of Unit
does not return data, but it does convey a single piece of information – that the function returns. This is in contrast to Nothing
– a function with a return type of Nothing
means the function doesn’t actually return.
Unit
is the type that contains only 1 value. It can be seen as a tuple of zero elements, and you can show that there is always a unique mapping of any type to Unit
. The name is used not only in functional programming, but also in category theory, and in number theory. The number 1 is neither prime nor composite, it is a “unit” - in the sense of “building block” - of the natural numbers. Other number spaces can have multiple units (like complex numbers having the units 1, -1, i, -i), but in a sense (regarding prime decomposition) all units are equivalent. The same holds for the Unit
type: You could define multiple unit types, but they are all structurally the same.
But programming is not category theory or number theory — and a name which makes good sense in mathematics may not do so in application programming.
And for general programming in Kotlin, the fact that Unit
is a type having exactly one value is not directly relevant. What’s relevant is that it’s a type for functions that return no useful value; a value that can be safely ignored; a value to be silently inferred by the compiler and then silently discarded by the compiler.
Of course, a single-valued type is a great way of implementing all those things, one that makes really good sense in the type system and makes everything work together very neatly. But it’s an implementation detail — and one that doesn’t have to ‘leak out’ in the name.
To pick another example, Kotlin also uses the empty type, a type with no values at all, as a type for functions that never return normally. That also works very neatly. But the designers didn’t call that type Empty
— or, even worse, Bottom
! While those would have been clear to all the category theorists and language designers, it would have made some code extremely confusing to read. Instead, they called it Nothing
— which makes perfect sense to developers and gives clear, readable code.
In exactly the same way, they could have used a different name for the unit type, one that would make better sense to developers and give more readable code, even if it’s not what would be most natural to category theorists.
Now, I’m not sure what such a name might be, but there must be some name more relevant than Unit
! Any suggestions?
(Many languages use a Void
type, but doesn’t read well — and is often the empty type… ()
is also used in many languages, but again, doesn’t read well, and doesn’t fit Kotlin’s identifier syntax. Python’s None
seems too similar to Nothing
. Trivial
might also mean more to mathematicians than to developers. Would something like Return
be confusing? Traditionally, a function returning no useful values would be called a ‘procedure’, though that doesn’t suggest a return type.)
It occurs to me that Kotlin already has another single-valued type: Nothing?
, which has the single value null
. That might make some sense — a function returning Nothing?
would clearly either return nothing (i.e. not return), or return null
. Perhaps for readability you could create a type Null
which was typealiased to Nothing?
. Then a function returning Null
would both conceptually and literally return a null value!
(Of course, this is all theoretical — Kotlin is far too mature for such a change to be considered. But it’s still interesting to speculate.)