I'd like to implement a generic F# class whose type parameter for sure provides a static method called "TryParse". Apart from that I want my class to get disposed correctly after not needed anymore. I've come up with the following implementation:
type Listener<'a when ^a : (static member TryParse : string -> ^a option)>() =
// construct the object here
let input : string = "" // get input
let res = (^a : (static member TryParse : string -> ^a option) input)
member this.Start() =
// ...
()
interface IDisposable with
member this.Dispose() =
// do cleanup
()
The thing is: on both of the members ("Start" and "Dispose") I get the following error:
Error: This code is not sufficiently generic. The type variable ^a when ^a : (static member TryParse : string -> ^a option) could not be generalized because it would escape its scope.
I can fix it on Start() member by decorating it with "inline", but there's no way I can do the same with the interface definition.
Is it possible to both enforce my generic type to implement a static method and define the class Disposable ?