我刚开始第一次使用浮点以外的度量单位。
在开始复杂之后,我发现您可以在多种“类型”中重复使用度量单位。
[<Measure>] type mm
let a = 1<mm>
let b = 1.<mm>
现在我坐在这里想知道如何更新我现有的 mm 类型以反映这种可能性:
[<Measure>]
type mm =
static member perMetre = 1000.0<mm/m>
static member fromMetre (v:float<m>) = v * mm.perMetre
static member toMetre (v:float<mm>) = v / mm.perMetre
static member lift (v:float) = v * 1.0<mm>
问题:我如何编写一个可以转换mm
为m
(比如说)可以处理int<mm> -> int<m>
AND的函数float<mm> -> float<m>
?
更新
感谢 Kurt,这是新版本:
[<Measure>]
type mm =
static member private perMetreFloat = 1000.0<mm/m>
static member private perMetreInt = 1000<mm/m>
[<OverloadIDAttribute("float")>]
static member fromMetre (v:float<m>) = v * mm.perMetreFloat
[<OverloadIDAttribute("float")>]
static member toMetre (v:float<mm>) = v / mm.perMetreFloat
[<OverloadIDAttribute("float")>]
static member lift (v:float) = v * 1.0<mm>
[<OverloadIDAttribute("int")>]
static member fromMetre (v:int<m>) = v * mm.perMetreInt
[<OverloadIDAttribute("int")>]
static member toMetre (v:int<mm>) = v / mm.perMetreInt
[<OverloadIDAttribute("int")>]
static member lift (v:int) = v * 1<mm>