How can I define an extension member on an F# unit of measure?

别等时光非礼了梦想. 提交于 2019-12-23 07:35:28

问题


Leaving aside whether we should use units of measure for unitless concepts like angles, suppose I have define degree and radian units in F#

type [<Measure>] degree =
    static member ToRadians (d:float<degree>) : float<radian> = d * (Math.PI * 1.<radian>) / 180.0<degree>
and [<Measure>] radian =
    static member ToDegrees (r:float<radian>) : float<degree> = r * 180.0<degree> / (Math.PI * 1.<radian>)

I can use them relatively easily like

4.0<degree> |> degree.ToRadians

It seems like extension members would be even handier. So I could just say

let d = 4.0<degree>
let r = d.ToRadians()

But I can't define the extension member the obvious way

type float<degree> with
    member degrees.ToRadians() = degree.ToRadians(degrees)

... this gets me the following error

error FS0010: Unexpected identifier in type name. Expected infix operator, quote symbol or other token.

Is there a syntactic trick for extension members on units of measure in F#, or is the feature supported?


回答1:


F# extension members are different from C# extension members in that you can't define extensions of constructed generic types. For instance, you can define extensions on seq<'t>, but not seq<int>. In other words, extension members really act like members of the type, rather than static methods. This applies to measure types too, so you can't define an extension on float<degree>, but you can define an extension on float<[<Measure>]'u>:

type float<[<Measure>]'u> with
    member f.Squared() = f * f

[<Measure>]
type m

let area = 2.0<m>.Squared()

However, I don't see how this helps you in your case...



来源:https://stackoverflow.com/questions/7153930/how-can-i-define-an-extension-member-on-an-f-unit-of-measure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!