开发者

Overloading of F# Measures

开发者 https://www.devze.com 2023-03-20 05:48 出处:网络
Consider the following F# code: [<Measure>] type pixel [<Measure>] type inch [<Measure>] type dot

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

精彩评论

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