I wrote up a draft explanation of how custom numbers work in the new compiler: https://gist.github.com/rtfeldman/f4f1ee9b0c8194e2e3d7309059bef7a4
the idea is for this to end up in the language reference.
any feedback welcome!
Reads well :)
I assume going with Ratio over Fraction was a deliberate choice?
In my opinion you can cut " which would be the requirement if div assumed both arguments had the same type (like they happen to in number types like Ratio and Dec, but not in number types like Duration)."
Anton said:
I assume going with Ratio over Fraction was a deliberate choice?
yeah that's what Clojure calls it
Anton said:
In my opinion you can cut " which would be the requirement if div assumed both arguments had the same type (like they happen to in number types like Ratio and Dec, but not in number types like Duration)."
fair, although I wanted to call that out because it's how all the builtin number types happen to be shaped, so I wanted to be clear that it's an intentional decision not to be that restrictive
I'm hyped for custom numbers! One of the first things I want to try out is making a 2D Projective Geometric Algebra number type for making really fast and precise physics calculations. I've got this idea that it would be cool if the graphics platform and associated packages for making games just used a nicer mathematical primitive rather than forcing game devs to think in terms of the lower level abstractions like how they efficiently represents vectors.
This idea may not go anywhere... but from the YT rabbit holes I've been down I'm feeling hopeful it will be nicer to work with.
This looks really neat by the way:
duration : Duration
duration = 5.Min + 30.Sec
Maybe that should be higher up as a hook for the reader :p
yeah maybe could note some things up top that you can do with it
and then explain how they all work
I love a good TLDR or Exec Summary :smiley:
Very exciting to read! Have you thought about using Tags for telling the negativeness? The is_negative associated functions is very clear, just that looking at this line, I need the line above it to know what the first parameter should be: Numeral.init(False, [], numeral.digits_after_pt()). Maybe init could accept the [NonNegative, Negative] tag union?
I know my comment's mostly about looks. The semantics are awesome and powerful!
I gotta question: can this be used for "tagging units" like metric stuff in a physics sim? I've never found an ergonomic solution; does Roc have something else that already works well? maybe tagged unions somehow?
Yeah the metric stuff is one of the main motivations that led to this design, like 12.Meters or 43.Psi
new question: does octopus mean roughly "awesome!" because that's what it means to me
what's the 2nd best way to do units (e.g. metric) in roc, using old features?
Rick Hull said:
what's the 2nd best way to do units (e.g. metric) in roc, using old features?
I made a port of elm-units a while ago: roc-units
I'm excited to port it to use custom numbers when the new compiler is ready :grinning_face_with_smiling_eyes:
Looks great! I love this style of implementation, mostly declarative and definitional, and obviously correct, with really nice things falling out of it. I'm not a license nerd, nor do I want to be, but I'd be tempted to reword "partly based on" to "inspired by". :+1:
To give some outside perspective on the 5.Sec + 3.Min API: in the latest C# version (14, released last year) this is also possible using extension members, where one can write 5d.Sec. Of course, this is evaluated at runtime, so not quite as cool :)
There is even a quite popular package which does this: https://github.com/angularsen/UnitsNet?tab=readme-ov-file#extension-packages
To me, it is cool to see how similar ideas for ergonomic APIs emerge in different contexts.
Rick Hull said:
I'm not a license nerd, nor do I want to be, but I'd be tempted to reword "partly based on" to "inspired by". :+1:
I believe the BSD 3 clause licence is one of the most permissive licences, if you make a derivative work then it only requires you to include a copy of the original licence with your derivative and to not imply that the original project endorses your derivative. I included a copy of the original licence and I think the names are different enough to not confuse anyone.
If thousands of companies are allowed to include BSD licenced code verbatim in their proprietary projects then I think I'll be okay translating a BSD licenced codebase and re-licencing it :sweat_smile:
Last updated: Feb 20 2026 at 12:27 UTC