Type-safe dimensional analysis and units of measure

“units-of-measure” is a Kotlin library for handling dimensional analysis and unit conversion in a type-safe manner.

What does that mean? (Source: The Trouble with Doubles)
In “conventional” code, where quantities are declared using ‘double’ or ‘int’, the following is completely valid:
[INDENT]

val sumMistake = xMilesPerHour + yMetres
val speedMistake = distance * time

These types of bugs can be very difficult to diagnose/replicate (e.g. when in deeply nested if-statements).
Using Kotlin’s type system, “units-of-measure” can prevent these errors at compile-time.[/INDENT]

Why is it important? (Source: Personal experience with the Squants library in Scala)
Type-safe units of measure and dimensional analysis is extremely beneficial:

  • Faster Development — IDE autocomplete will provide meaningful predictions, rather than listing every number in scope.
  • Cleaner Code — You no longer have to add units to your variable names, that information is documented by the type.
    
  • Safer — Mistakes will show up at compile time, rather than while physically running the robot.
    
  • Informative — Type-safe calculations are helpful for newer members who may not have a strong physics understanding or are unfamiliar with the code base.
    

Kotlin is an alternative JVM language that is gaining popularity in FIRST. While type-safe unit libraries exist in C++ and Scala, there was previously no elegant solution for Kotlin.

More information can be found on the GitHub repository and on the wiki. Please email me if you would like some help using this library, I’d be happy to arrange a meeting. Your feedback is greatly appreciated.

Note: While predefined units come out-of-the-box, I recommend using “units-of-measure” in “custom mode.”