2

我需要能够用 F# 中的几个不同单位来表示相同的概念。例如,我想用光年、天文单位、公里和米来表示“距离”。我想使用通用函数对这些值进行计算。这就是我将 ly、AU、km 和 m 组合在一起的方式:

[<Measure>] type ly
[<Measure>] type AU
[<Measure>] type km
[<Measure>] type m

[<Measure>] type distance

type UnitValue<[<Measure>] 'u, [<Measure>] 't> =
    val conversionFactor : float<'t / 'u>
    val value : float<'u>
    new (v, cf) = { value = FloatWithMeasure<'u> v; conversionFactor = FloatWithMeasure<'t / 'u> cf }
    member this.toUnits = this.value * this.conversionFactor
    member this.fromUnits (x : float<'t>) = x / this.conversionFactor
    static member (+) (a : UnitValue<'u, 't>, b : UnitValue<_, 't>) =
        a.newValue (a.toUnits + b.toUnits)
    static member (-) (a : UnitValue<'u, 't>, b : UnitValue<_, 't>) =
        a.newValue (a.toUnits - b.toUnits)
    static member (*) (a : UnitValue<'u, 't>, b : float) =
        a.newValue (a.toUnits * b)
    member this.newValue (x : float<'t>) =
        new UnitValue<'u, 't>(float (this.fromUnits x), float this.conversionFactor)

//Distance units
type LightYearValue(value) =
    inherit UnitValue<ly, distance>(value, 6324.0)

type AstronomicalUnitValue(value) =
    inherit UnitValue<AU, distance>(value, 15.0)

type KilometerValue(value) =
    inherit UnitValue<km, distance>(value, 0.00001)

type MeterValue(value) =
    inherit UnitValue<m, distance>(value, 0.0000000)

这段代码是从不知道单元的 C# 中调用的,只需指定 即可完成new LightYearValue(4.2),这将成为UnitValue<ly, distance>F# 中的 a,并且可以传递给期望 a 的函数UnitValue<_, distance>。这样,适当的单元进入函数,适当的单元退出。例如,如果我传递了函数 a UnitValue<AU, distance>,我可能会float<AU / s ^ 2>根据计算得到 a ——这将是一个适合比例的数字。

对此感到非常满意,我开始编写 Orbit 类型:

and Orbit(PeR : UnitValue<_, distance>, ApR : UnitValue<_, distance>, AgP : float, focus : SphericalMass) =
    let PeR = PeR
    let ApR = ApR
    let AgP = AgP
    let focus = focus
    let Maj = PeR + ApR
    let Ecc = (Maj.value - (2.0 * PeR.value)) / Maj.value
    let DistanceAt theta =
        (Maj.value / 2.0) * (1.0 - Ecc ** 2.0) / (1.0 + Ecc * Math.Cos(theta))

但是当我将鼠标悬停在上面时PeR,它说它的类型是UnitValue<1, distance>. 那么给了什么?为什么这不起作用?我可以编写一个函数UnitValue<_, distance>,它工作正常!它可能与 C# 与此代码交互有关吗?(类型由 C# 类扩展)有什么办法可以使这项工作:(

4

1 回答 1

3

声明类型时,需要显式声明泛型类型参数(以及单元参数)。以下声明正确地推断出类型:

type Orbit<[<Measure>] 'u, [<Measure>] 'v> 
    ( PeR : UnitValue<'u, distance>, ApR : UnitValue<'v, distance>,
      AgP : float, focus : SphericalMass) =
  let Maj = PeR + ApR
  let Ecc = (Maj.value - (2.0 * PeR.value)) / Maj.value
  let DistanceAt theta =
      (Maj.value / 2.0) * (1.0 - Ecc ** 2.0) / (1.0 + Ecc * Math.Cos(theta))

(顺便说一句:您不需要为本地let绑定重新分配参数 - 它们将可以自动访问,所以我删除了类似的行let ApR = ApR

于 2011-03-11T09:53:10.653 回答