Ha.nnes.dev

Roc Units

📏

Type-safe units of measure in Roc

2024-05-21

Type-safe units of measure in Roc.

Example

Feet, inches and centimeters are all Lengths, so they can all be added to and converted to each other.

height = Quantity.feet 5 |> Quantity.add (Quantity.inches 11)
Stdout.line! "Taylor Swift is $(height |> Quantity.toCentimeters |> Num.toStr) centimeters tall."
Taylor Swift is 180.34 centimeters tall.

But incompatible units cannot be used together.

isThisAKilometer = Quantity.kilograms 1 |> Quantity.add (Quantity.meters 1)
This 2nd argument to add has an unexpected type: ...

For a list of all the supported units, see the documentation.

Licence

This project is released under the UPL licence and is partly based on the elm-units library, which is licenced under the BSD 3-Clause License.