I\'m attempting to use explicit member constraints in F#. The documentation says \"F# supports the complete set of constraints that is supported by the common language runti
You need to mark the member inline
(and add type annotations if you want to force the arguments to be of type ^T
):
type MyType<'T when ^T: (static member ( + ) : ^T * ^T -> ^T)>() =
member inline this.F (a:^T) (b:^T) = a + b
I've also added a constructor, so that you can actually call the method:
MyType().F 1 2
As others have noted, it is rarely necessary to write out the explicit member constraints by hand since they will usually be inferred. Furthermore, in this case there's no reason to put the constraint on the type rather than the method, and having a type parameterized by a statically resolved type variable is not idiomatic.