开发者

Overloading of F# Measures

Consider the following F# code:

[<Measure>] type pixel
[<Measure>] type inch
[<Measure>] type dot
[<Measure>] type percentage

let scaleCalculation (finalSize:float<pixel>) (originalSize:float<pixel>) =
   finalSize/originalSize * 100.0<percentage>

(I realize I need to check originalSize for 0 but that's not really germaine to this question).

What I'd like is to overload this function to handle inches开发者_如何学运维 and dots per inch. I don't think there's any way to overload on the unit of measure but I just thought I'd see if anyone had any suggestions on this.

I know I could do this:

   let scaleCalculation (finalSize:float) (originalSize:float) =
      finalSize/originalSize * 100.0<percentage>

but then I lose checking on the measure of finalSize and originalSize. I just want to insure that the measure of finalSize and originalSize are the same.

Any suggestions, thoughts?


let scaleCalculation (finalSize:float<'u>) (originalSize:float<'u>) =
   finalSize/originalSize * 100.0<percentage>

Units of Measure in F#: Part Four, Parameterized Types

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜