问题
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