F# Ununit - reunit inside a function

前端 未结 2 1208
遥遥无期
遥遥无期 2020-12-21 09:58

This question is closely related to these ones (1, 2, 3)

I\'m using an external library which doens\'t (yet) handle units of measure. I want to be able to \

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-21 10:14

    For now, boxing and casting appears to work:

    let MyUnitAwareClient (s:float<'u>) =  
      let result = s |> float |> ExternalNonUnitAwareFunction
      (box result :?> float<'u>)
    

    I wouldn't be surprised if the units of measure stuff goes through some further changes before release, though, which might break this. You can also make a more generic version, such as:

    let reunit (f:float -> float) (v:float<'u>) =
      let unit = box 1. :?> float<'u>
      unit * (f (v/unit))
    

    EDIT

    There is now a FloatWithMeasure function to 'cast to units':

    http://msdn.microsoft.com/en-us/library/ee806527(VS.100).aspx

提交回复
热议问题