Question

I'm noobing around with F# trying to create overloads of cos that accepts angles with units.

This is my code:

[<Measure>] type rad
[<Measure>] type deg
let toRad(x:float<deg>) =
    (float x) * 3.14159265 / 180.0
    |> LanguagePrimitives.FloatWithMeasure<rad>
let cos (angle: float<rad>) = cos(float angle)
let cos (angle: float<deg>) = cos(toRad angle) // get duplicate definition of cos here

Compiler complains about duplicate definition of cos on the last row.

Was it helpful?

Solution

Measure types are erased (see the specification), so you effectively have two definitions of cos(angle: float) which causes the error.

You could create a union type for the two possibilities

type Angle = Degrees of float | Radians of float

or give the functions different names.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top