4 ms·
Units should be built into languages! Others have pointed to some libraries that make this possible in C++ and Rust; I'm sure it exists for other languages too.
by liminal 6y ago
Units should be built into languages! Others have pointed to some libraries that make this possible in C++ and Rust; I'm sure it exists for other languages too. But this seems still short of what's needed: Every number in an application should be of a certain type and those types should combine on mathematical operations. Often those types already exist within the application's type model -- i.e. every class/interface is an obvious candidate. Then it would just be a matter of assigning those types to numbers. This would yield the ability to express things like Users/Client or Requests/second through simple math operations on variables and have it all type checked.
- fuball63 6y agoAda does this well with its concept of subtypes; so you could subtype Int to your unit, and even give it valid ranges. It's then typechecked by the compiler.
- db48x 6y agoIt's a lot harder to do well than it looks, mostly because unit systems were invented by humans and have a lot of aggravating edge cases and oddities. For example, most units can be converted to another unit of the same dimensionality by mulitiplication or division (such as length in inches * 2.54 == length in millimeters), but then there's temperature where you also need to add or subtract an offset. It's almost as if humans spent thousands of years inventing units without ever once considering how difficult they might be to implement in a computer programming language.
- Symbiote 6y agoAre there any edge cases if we just stick to SI units? Most of the world doesn't care about the thousand-year units, and the bit that does could just convert to them on output.
- db48x 6y agoTemperature, times, dates, cgs vs mks, dimensionless quantities, angles, the list goes on and on. And that’s before you get into the technical details of how you store units and conversion factors in memory, how you handle precision when mixing large and small units, how you interact with unlimited‐precision and rational numbers, how you use or avoid the type system in your programming language, how users of your unit library define their own units… Here’s a good write‐up of what they’re thinking about for future versions of C++: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1935r2.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p193.... It’s worth a read even if you don’t write in C++. And if you bake it into your language, you’d better get it all right the first time, because you probably won’t be able to fix it later.
- egl2020 6y agoF#