Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise Unit of Measure type to specificed power

Tags:

f#

Is it possible to somehow create a pow function for measure types? The pow function in f# only takes int as parameter, and then pow function in the Math class takes a float - but dosent allow float<cm>.

I first thought that:

let rec myPow(x:float<cm>,y:int) =
    if y = 0 then x
    else myPow(x*x, y - 1)

might work out, but its obvious that each time it come across the else line it will change the return type.

Any suggestions?

like image 236
ebb Avatar asked May 11 '11 11:05

ebb


1 Answers

I don't think that is possible. You are asking the function to return <cm^2> in case the power is by 2 and <cm^3> in case of 3 and so on. Which makes the function to return different "types" based on the calculation which obviously not possible in a static type and type safe language. Unfortunately, I don't think units of measure can be made "generics" to try that to reach any further.

Your function can have only one static return type.

like image 74
Ankur Avatar answered Oct 13 '22 04:10

Ankur