Unit is the worst naming

The word ‘unit’ is used very widely in the real world.
For example, A Quantity class consists of ‘amount’ and ‘unit’.

data class Unit(
    val code: String,
    val name: String,
    val symbol: String
)

data class Quantity(
  val number: Number,
  val unit: Unit
)

val METER: Unit = Unit("m", "meter", "M")
val GRAM: Unit = Unit("g", "gram", "M")

val heightOfBuilding = Quantity(500, METER)
val weightOfBeef = Quantity(500, GRAM)

I can’t use Unit because Kotlin occupied it.
There is no suitable word for ‘a standard amount of a physical quantity’ except Unit.

1 Like

That’s what packages are for.

StandardAmountOfAPhysicalQuantity is also available if you wish

3 Likes

That is unfortunate. You could potentially use instead:

  • Dimension
  • MeasurementUnit
  • Magnitude

I definitely wouldn’t recommend it, but if you are dead-set on the Unit naming, you could import kotlin.Unit as KUnit to avoid the namespace collision.

2 Likes

I chose Uom(Unit of Measure).

2 Likes

Using full names instead of initialisms makes them more readable, and your IDE’s code completion makes them pretty easy to type. I would suggest you use UnitOfMeasure as your class name.

6 Likes

I have to agree. And there is this disregard for units of measure across almost all other languages and standard libraries. It’s almost like software engineers have a prejudice against actual science. I have never quite understood.

“Unit” is a term from type and category theory, it is the representation of all types with exactly one element, and there is always a unique mapping from any type to unit. In a sense, “Nothing” (or “bottom”) is kind of its opposite, because you can uniquely map Nothing to every type.

There will be always some words you would like to use (e.g. I’m often annoyed that “is” and “as” are off limits for my DSLs). I agree that the overlap in meaning with physical and mathematical units is unfortunate, but in my opinion “Unit” expresses much better what is going on than the “void” inherited from C. Making it a “real” type was a huge improvement over the special handling that “void” needed, so I’m all for naming it appropriately, even if it is a minor inconvenience in cases like yours.

5 Likes

What I find odd about the name ‘Unit’ is that Kotlin has generally prioritised being clear and easy to read, even when that means not following existing usage in mathematics, computer science, and/or previous languages. (For example, fun instead of ‘def’, Nothing instead of ‘bottom’ or ‘void’, Any instead of ‘top’ or ‘Object’…)

But Unit seems very much a term from mathematics. In general usage it makes only the vaguest sense, isn’t very intuitive, and doesn’t read well. (As shown by the confusion it seems to generate, and all the questions about it on sites like this.)

Maybe JetBrains just couldn’t think of anything better? (I’m not saying that I can… But if they had, then code would read better and the word would be free for the scientific meaning!)

4 Likes

The Unit type ist a well known term in functional programming, e.g. Scala uses it. I think it was sensible by JetBrains to keep it, instead of reinventing the wheel.

2 Likes

For me personally, one of the main selling points of Kotlin was to be “Scala, but for humans”. “Unit” is a great example of things that stopped me from getting into Scala and Kotlin is free of most of such stuff.

But I agree that maybe there wasn’t a better word. Type system is by nature an abstract thing and speaking about it is going into the academic territory already. We can’t avoid this easily.

3 Likes

As somebody who has avoided Scala like the plague, this is only for a small set of users better. For example in your case. However, ‘Unit’ for me does not ring a bell at all other than a Unit of Measurement.
According to Kotlin:

In Kotlin, Unit is a type that represents a function that does not return any meaningful value. It is similar to void in Java and other languages but has some differences and specific uses in Kotlin. Let’s explore Unit in detail:

So basically They are returning a unit of something that is nothing?? I find that confusing, Scala is also confusing

No, they return a very concrete “something”, but this something is usually ignored, because there is only one value for this type, and this value holds no data. So, you can think of it as: “This method returns something, but the result is so boring that you can safely ignore it.”

So why bother? I can see at least two reasons:

  • “Something” is not “nothing”. If you had a method in Java that would never return (infinite loop or throwing always an exception), you would give this method the same return type void like a “normal” method. In Kotlin you can do better, and distinguish it from normal methods by using Nothing instead of Unit. E.g. you have probably come across the TODO() function, and it only works because of this distinction.
  • Returning a rather boring, but regular value helps a lot when using generics. In Java you had all these special cases handling “no return value”, like the interface Consumer - in Kotlin it’s just a normal function returning Unit. Kotlin does more interesting stuff with generics, like more functional programming and extension methods, that makes it more important to avoid this quirk (similar as it is avoiding the problems Java has with primitive types as generic parameters)

Don’t think Unit is the same as void, it really isn’t. It is a very normal type with a single value (also named Unit) that helps to make the type system more regular. You can assign it to variables, it can be an argument, there is nothing special about it, except that you don’t have to return it explicitly in a function, and that it is exceptionally boring.

2 Likes

I understand and respect the language designer’s intention. Unit is scientific and rational, but not practical nor user-friendly.
In my project, I have written down about 100K lines, but used Unit less than 10 times. In addition, the word ‘unit’ is widely used not only in the computer science world, but also in the real world. I tried to find alternative words of unit but failed.
I love Kotlin’s sense of humor. Whenever I type ‘fun’, I have fun. Unit makes Kotlin scientific, but hard to use in science.