Units of Measure - reuse method with different units passed in
I have a method, called RateOfChange
, that takes two values that are read one second apart, and returns the result.
For two positions, it returns the velocity, for two velocities, it returns the acceleration, for two energy values it returns Joules/second etc.
This is physically possible, but Units of Measure don't let me do that - on first use the method is contrained to a specify type:
let RateOfChangeWithTime (value1, value2) = (value2 - value1) / 1.0<SI.s>
let velocity = RateOfChangeWithTime(2.0<SI.m>, 1.0<SI.m>)
let acceleration = RateOfChangeWithTime(3.0<SI.m/SI.s>, 2.0开发者_JS百科<SI.m/SI.s>)
The line velocity =
causes the method to be constrained to float<SI.m> -> float<SI.m>/float<SI.s>
, which of course is correct for velocity, but the line acceleration =
... then fails to compile as it is passing in a float<SI.m/SI.s>
which the method doesn't expect.
I'd want RateOfChangeWithTime
to be invariant to the type passed in, but just return a unit of measure which has been divided by seconds.
This would seem to more match real live situations, is this possible to do? (I'm trying to do this for a more complicated scenario - see here (http://taumuon-jabuka.blogspot.com/2010/11/f-units-of-measure-with-reactive.html)
let RateOfChangeWithTime (value1: float<_>, value2: float<_>) = (value2 - value1) / 1.0<SI.s>
let velocity = RateOfChangeWithTime(2.0<SI.m>, 1.0<SI.m>)
let acceleration = RateOfChangeWithTime(3.0<SI.m/SI.s>, 2.0<SI.m/SI.s>)
精彩评论