测量单位 - 传入不同单位的重用方法
我有一个名为 RateOfChange
的方法,它采用相隔一秒读取的两个值,然后返回结果。
对于两个位置,它返回速度,对于两个速度,它返回加速度,对于两个能量值,它返回焦耳/秒等。
这在物理上是可能的,但测量单位不允许我这样做 - 第一次使用方法被限制为指定类型:
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<SI.m/SI.s>)
velocity =
行导致该方法被限制为 float
,这对于速度来说当然是正确的,但是 acceleration =
... 行在传入时无法编译该方法不需要的 float
。
我希望 RateOfChangeWithTime 对于传入的类型保持不变,但只返回除以秒的测量单位。
这似乎更符合现实生活中的情况,这可能吗? (我正在尝试针对更复杂的场景执行此操作 - 请参阅此处(http://taumuon-jabuka.blogspot.com/2010/11/f-units-of-measure-with-reactive.html)
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<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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)