3 ms·
I think this is a really good example of where languages should have built in support for units. I was just having a look around and found the units module for
by samwillis 13y ago
I think this is a really good example of where languages should have built in support for units. I was just having a look around and found the units module for python (https://pypi.python.org/pypi/units/ https://pypi.python.org/pypi/units/). I really like the example at the end where they have modified pypy for a native unit syntax.
- gizmo686 13y agoI think built is support for units is a bit to specific. Support for cheap type alias'es would work, and be far more general. What I mean is something like: type Inch = int type Centimeter = int And then have the compiler enforce these as different types (as opposed to alias'es), so if you want to use an Inch, where the function decalares it expects a Centimeter, you would be force to cast it (or have the compiler auto cast using your conversion function). This doesn't get you support for multidimensional units (IE, define a function that goes from arbitary unit u to u^2), which would require a much more complicated addition to the type system.
- dragonwriter 13y ago> I think built is support for units is a bit to specific. Support for cheap type alias'es would work, and be far more general Go has support for cheap type aliases for which names typed with the same underlying type are distinct types which are not mutually assignable, and, in fact, Duration is such a type with int64 as the underlying type.