3 ms·
It would actually be pretty nice if this: 26.5 meters + 56.3 dollars failed to compile. In Scala one can certainly build this. You could certainly do it
by yummyfajitas 14y ago
It would actually be pretty nice if this:
26.5 meters + 56.3 dollars
failed to compile.
In Scala one can certainly build this. You could certainly do it in Haskell, though it wouldn't be as nice syntactically.
- Game_Ender 14y agoIn C++11 that would be: 26.5_meters + 56.3_dollars
- MichaelGG 14y agoF# has units of measure[1], which lets you use units on numeric types. It also does unit conversion, so if you have a function (x * x), the type sig would be "x:int<m> -> int<m ^ 2>". Pretty neat. Without the neat inherent aspects, any language with lightweight support for phantom types can achieve some level of this functionality. For instance, you could define a sum type with only one case taking the type to wrap. Thus you can easily have TransactionId, AccountId, etc. as distinct types, without spending more than a line to declare them. 1: http://msdn.microsoft.com/en-us/library/dd233243.aspx http://msdn.microsoft.com/en-us/library/dd233243.aspx
- danbruc 14y agoThis should be easy in many languages - inherit from a numeric type or wrap one and overload the operators. This is actually not uncommon - in areas with high risk potential this is standard practice up to using different data types for x, y and z coordinates to avoid that your flight control accidentally adds wrong things together.
- tikhonj 14y agoThere are a couple of packages in Haskell that let you do this, including unittyped[1]. It lets you write code like: *Main> 2 meter + (1 meter / second) * 5 second 7.0 m A really cute feature is that prefixes like centi and deci are functions: *Main> gallon `as` (cubic (deci meter)) 4.546089999999999 dm⋅dm⋅dm⋅# It gives you an error if the dimensions don't match, but unfortunately the error messages are hideous. Anybody can define new units just by giving a conversion factor and a string to display. These new units immediately work with other units. You can probably define new "dimensions" as well, but that's bound to be more involved. All the units are represented as empty types, so it should have no overhead at runtime. So it is very nice syntactically, with the only real problem being the error messages and the fact that it uses a whole bunch of extensions and only supports very new versions of GHC. I think it's pretty cool.
- cynwoody 14y agoAccording to Google, 1 gallon is 3.78541178 cubic decimeters[1]. [1] https://www.google.com/search?q=1+gallon+in+cubic+decimeters https://www.google.com/search?q=1+gallon+in+cubic+decimeters
- tikhonj 14y agoIt's an imperial gallon[1]. As to why it's an imperial gallon, or what an imperial gallon really is, or who uses it--don't ask me. I'm a very metric sort of person :P. [1]: https://www.google.com/search?q=imperial+gallon+in+cubic+decimeter https://www.google.com/search?q=imperial+gallon+in+cubic+dec...
- andrewflnr 14y agoYou will probably find the Frink language interesting: http://futureboy.us/frinkdocs/ http://futureboy.us/frinkdocs/. http://futureboy.us/frinkdocs/#SampleCalculations http://futureboy.us/frinkdocs/#SampleCalculations further down is fun, especially the later ones.