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?
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 notseq<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 onfloat<degree>
, but you can define an extension onfloat<[<Measure>]'u>
:However, I don't see how this helps you in your case...