What does HackerNews think of Unchained?

A fully type safe, compile time only units library.

Language: Nim

#201 in Hacktoberfest
#7 in Nim
Yes. The kind of type modeling this is doing is not very physics-unit-minded at all (where any multiplicative operation potentially creates a new type). Rather, it is trying to prevent non-additive (add/subtract/compare) operations bugs viewed as common/high risk.

I mean, your example in the context of these dim.analysis discussions would really have an output type of DurationSquared, not Duration which is what usual prog.lang modeling does for arithmetic ops.

Go also is not alone. E.g., the Nim std/times has a `*` defined for (int, Duration) but not (float, Duration).

Personally, I suspect prog.langs with native 64-bit ints are better off just using raw nanoseconds for this kind of thing (even time since the epoch gets you into Star Trek centuries.. something like 10 human generations from now).

In terms of the type creation, though, there is some hope like the Nim Unchained package: https://github.com/SciNim/Unchained

Anything with a finite (200 is small even) number of units misses the algebraic structure of the problem mentioned in https://news.ycombinator.com/item?id=36988497 wherein multiplying|dividing two things gives you a (potentially) new unit which implies an open ended "space" of units.

To be concrete (hah!), in C++ a template meta-type with 12 signed integer parameters (6 numerators & 6 denominators for rational exponents of SI base units) might be one way to model it.

Unlike C++ template stuff, Nim macros (like Lisp macros) makes metaprogramming more like procedural programming - just against abstract syntax trees. I think that helps to shield some of this type complexity from users, but the documentation README https://github.com/SciNim/Unchained does better job than I can in an HN comment.

Of course, for unit system conversion, the number of dimensions (6 in SI, 3 in CGS/Gaussian) changes. So, for full generality you need compile-time (if you want static type integration/CT errors) linear algebra over a rational field (at least & conventionally) to project|inverse project. That might be theoretically possible in C++. I would think it very un-fun and unlikely to ever have been done. There's probably a Mathematica package, though.

EDIT: It seems that mp-units mentioned in a sibling comment has some rare support for varying unit systems (no GR or Planck ones, but at least "natural" & "hep" & not sure about system-conversion or just system-use). That lib also leans very heavily on many very modern C++ (C++20 & beyond) features, has constexpr everywhere in use cases. I am not sure I would say it shields users wonderfully or makes it seem like units are very ordinary types. Just looking at mp-units/example/VARIOUS.cpp makes it look syntactically very noisy, verbose, and a lot to learn.

Nice, that looks very interesting. I'll remember it in case I need to do some C++ work in the future again!

I developed a similar library for Nim, `unchained` [0]. Thanks to Nim's extensive macro system it's a pleasure to use (from a short look at the `au`'s docs my biased opinion is it beats `au` on usability :) ).

[0]: https://github.com/SciNim/Unchained

I've recently written a units library for Nim [0]. It's still WIP, but it's already proven extremely useful for me as a physicist.

Thanks to Nim's strong type system and metaprogramming features, it allows for a fully compile time design, without any runtime overhead (in form of special unit objects or such things; everything is a `distinct float`).

In addition Nim's unicode support, the code even looks nice!

A more complex use case (I can link more if desired): [1]

[0]: https://github.com/SciNim/Unchained/

[1]: https://github.com/SciNim/Unchained/tree/master/examples