Are there any programming languages that let you specify dimensional units as part of the type information? It would add to the analyzability of code at compile time.
Something like this: type Second;
type Minute = 60 Second;
type Hour = 60 Minute;
type Meter;
type Mile = 1609.344 Meter;
type DegreeC;
type DegreeF = DegreeC * 9/5 + 32;
float(Meter/Second) mps = (Mile/Hour)60; // converts Miles/Hour into Meters/Second
float(Meter/Second) x = (Hour)60; // type error
>>1
these are all derived classes of a unit-less number. And the definitions you gave correspond to constructor definitions.
Name:
Anonymous2012-02-25 17:16
Probably be able to do this using constructors, type classes, and (I don't know if haskell has this but) where you can make new datatypes of type classes. So:
data Second = Second Float
data Minute = Minute Float
-- etc.
class Time a where
-- i guess code to convert between the units or sth
instance Time Second where
--- ...
data Meter = Meter Float
data Time a, Distance b => Speed = Speed b a
c = Speed (Meter 3e8) (Second 1)
IDK, lol. I don't even know haskell can do this but probably this is how it would be done. Though it looks to be a pain. I'd rather just use matrices for everything.
On second though, nvm, haskell doesn't automatically coerce between types.
Sarah moaned as she slowly pushed the erect eggplant into her front port. Paralysed by terror, the eggplant tried to escape but its resistance only added to Sarah's pleasure.
Name:
32012-02-25 20:26
Although it is tricky. There are technically infinitely many types here. There is m, m/s, m/s^2. m/s^3. m/s^4... Infinitely many types. You couldn't hope to handle something like this in seeples. Maybe haskal can do it.
This can easily be done with C++ templates, and the new user defined literals in C++0x. ECMAScript 5 should also allow this style with user defined get/set implementations.
Name:
Anonymous2012-02-26 2:11
You can do it in GNU-C if you abuse the extensions.