News for hackers. Stuff that matters.
Re:A Dangerous Obsession with Primitives by thezilch on Thursday May 31, 2012 @01:19PM (#4049139)
I'm reminded of Bjarne Stroustrup's keynote "C++11 Style" that describes C++11's vision of "Type-rich Programming."

Slides: http://ecn.channel9.msdn.com/events/GoingNative12/GN12Cpp11S... -- "Type-rich Programming" comes in at slide 17.

Video (and Audio downloads): http://channel9.msdn.com/Events/GoingNative/GoingNative-2012... -- slide 17 comes in around a fifth of the way in, as the player does not appear to have minutes/seconds indicator. Nonetheless, one should be able to find their way using the slides (pdf) images in conjunction with the video to find their spot.

I don't have time to review the intro into those slides, if they are relevant, but the keynote is generally good anyhow, so I'd recommend viewing the whole piece.

Re:A Dangerous Obsession with Primitives by cpeterso on Thursday May 31, 2012 @02:32PM (#4049595)
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?

Re:A Dangerous Obsession with Primitives by timmaxw on Thursday May 31, 2012 @04:19PM (#4050124)
* 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/>.