3

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.

1

1 Answer 1

5

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.