Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 440
Onorio Catenacci Avatar asked Dec 21 '25 06:12

Onorio Catenacci


1 Answers

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

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

like image 183
gradbot Avatar answered Dec 23 '25 06:12

gradbot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!