Given the following fragment:
type Foo() =
static member Test (act : unit -> unit) = act()
static member Test (act : Action) = Foo.Test act.Invoke
I get an error on the final line stating: A unique overload for method ‘Test’ could not be determined based on type information prior to this program point. A type annotation may be needed.
Unfortunately, the type annotation (act.Invoke : unit -> unit) does not resolve the ambiguity, and I cannot find an annotation that does fix it. I would like the Action version of be a wrapper round the -> version. My particular use case is defining a class that will be called from both F# and C#, so I want it to work natively from both languages.
F# inserts the conversion to
Actionfor you, so you could (in theory) get by with one method.I can’t figure out how to force the compiler to choose between
Actionandunit -> unit. Two other options:Actionoverload the primary and doFoo.Test(Action(act))But my recommendation would be a single method taking
Action. Since the conversion is automatic, interop, in a sense, is free.