2

的,仍在 使用计量 单位 _

我刚开始第一次使用浮点以外的度量单位。

在开始复杂之后,我发现您可以在多种“类型”中重复使用度量单位。

[<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>

问题:我如何编写一个可以转换mmm(比如说)可以处理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>
4

1 回答 1

2

有时简单地重载有效(如果你有一个论点):

[<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
    [<OverloadIDAttribute("0")>]
    static member lift (v:float) = v * 1.0<mm>
    [<OverloadIDAttribute("1")>]
    static member lift (v:int) = v * 1<mm>
于 2009-09-30T10:38:02.890 回答