Search code examples
f#overloadingmeasures

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?


Solution

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

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