Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 332
Sebastian Good Avatar asked Aug 22 '11 21:08

Sebastian Good


1 Answers

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...

like image 58
kvb Avatar answered Nov 09 '22 22:11

kvb