Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I started to write a C++ template class that would implement strongly-typed ints (so Celsius and Fahrenheit types could behave like ints, but have distinct types).

I gave up after this "simple" idea approached 200 lines of code implementing all the operator overloads. I guess the lesson is that primitives are complex, even if you just want to give them a new name. Also, the expression int/int produces an int, but what should the expression FahrenheitInt/FahrenheitInt produce? A unitless int? A FahrenheitInt?



* Dividing or multiplying a fahrenheit temperature is meaningless, because 0ºF is arbitrary.

* Ideally, you want separate types for absolute temperatures and temperature-deltas. Absolutes cannot be added, multiplied, or divided; subtracting absolutes produces a delta; adding or subtracting an absolute from a delta produces an absolute; deltas can be added and subtracted, and multiplied by unitless values.

* Multiplying a MeterNum by a MeterNum would ideally produce a SquareMeterNum.

* If you don't have to worry about precision, MeterNum and FootNum should both be replaced by LengthNum.

* You can convert a LengthNum into, say, meters by dividing by whatever value represents "1 meter" to produce a unitless int.

* Often operators don't make sense at all. (a << b) only makes sense if b is a unitless integer, for example.

I implemented most of this (but not absolute temperatures) as a Python library. The source is at <http://timmaxwell.org/svn/src/pychem/units.py>; and <http://timmaxwell.org/svn/src/pychem/std_units.py>. (Warning: these will download rather than opening in the browser.) I also found this: <http://pypi.python.org/pypi/units/>.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: