Stream: show and tell

Topic: Roc Units


view this post on Zulip Hannes (May 22 2024 at 09:39):

Just wanted to share the roc-units package I wrote to try and understand the Roc type system a bit better by translating some of the elm-units package.

Here's an 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: ...

view this post on Zulip Richard Feldman (May 22 2024 at 11:28):

cc @Ian Mackenzie - check it out! :smiley:

view this post on Zulip Hannes (May 22 2024 at 12:23):

Oh nice, didn't know you were on the Roc Zulip @Ian Mackenzie , I hope you don't mind me blatantly stealing how elm-units works :sweat_smile: it's a really elegant design

view this post on Zulip Hannes (Feb 04 2025 at 05:39):

I've recently released two new versions of this package, v0.2.0 added some more US units like the teaspoon and pound-foot force (and completely rewrote how the codegen script works), and v0.3.0 added support for Roc v0.0.0-alpha2 :)

view this post on Zulip John Konecny (Feb 05 2025 at 16:15):

image.png


Last updated: Jul 06 2025 at 12:14 UTC