Joh*_*son 3 f# trigonometry units-of-measurement
我正在努力用F#试图创建cos接受单位角度的重载.
这是我的代码:
[<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
Run Code Online (Sandbox Code Playgroud)
编译器抱怨最后一行的cos重复定义.
测量类型被删除(参见规范),因此您实际上有两个定义cos(angle: float)导致错误.
您可以为两种可能性创建联合类型
type Angle = Degrees of float | Radians of float
Run Code Online (Sandbox Code Playgroud)
或赋予函数不同的名称.