# Unit object

**URL:** <https://discuss.kotlinlang.org/t/unit-object/27596>\
**Category:** Uncategorized\
**Created:** [November 6, 2023, 6:20pm UTC](https://discuss.kotlinlang.org/t/unit-object/27596 "2023-11-06T18:20:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![muppetboy333](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@muppetboy333](https://discuss.kotlinlang.org/u/muppetboy333)\
**Post date:** [November 6, 2023, 6:20pm UTC](https://discuss.kotlinlang.org/t/unit-object/27596/1 "2023-11-06T18:20:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ebrowne72](https://avatars.discourse-cdn.com/v4/letter/e/85f322/32.png) [@ebrowne72](https://discuss.kotlinlang.org/u/ebrowne72)\
**Post date:** [November 6, 2023, 7:24pm UTC](https://discuss.kotlinlang.org/t/unit-object/27596/2 "2023-11-06T19:24:07Z")

</div>

The accepted answer [here](https://stackoverflow.com/questions/22654932/what-is-the-purpose-of-unit-returning-in-functions) is from one of the designers of Kotlin.

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [November 6, 2023, 8:42pm UTC](https://discuss.kotlinlang.org/t/unit-object/27596/3 "2023-11-06T20:42:46Z")

</div>

The short version (from that link) is that it’s [“a traditional name”](https://stackoverflow.com/questions/22654932/what-is-the-purpose-of-unit-returning-in-functions#comment34551391_22655381).

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.

---

<div class="post-metadata">

**Author:** ![ebrowne72](https://avatars.discourse-cdn.com/v4/letter/e/85f322/32.png) [@ebrowne72](https://discuss.kotlinlang.org/u/ebrowne72)\
**Post date:** [November 14, 2023, 1:04am UTC](https://discuss.kotlinlang.org/t/unit-object/27596/4 "2023-11-14T01:04:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Landei](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/landei/32/9407_2.png) [@Landei](https://discuss.kotlinlang.org/u/Landei)\
**Post date:** [November 14, 2023, 9:58am UTC](https://discuss.kotlinlang.org/t/unit-object/27596/5 "2023-11-14T09:58:28Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [November 15, 2023, 11:27am UTC](https://discuss.kotlinlang.org/t/unit-object/27596/6 "2023-11-15T11:27:09Z")

</div>

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.)
