Ambiguity on overload resolution between Action and unit -> unit types in F#
f#
Solution
F# inserts the conversion to `Action` for you, so you could (in theory) get by with one method.
type Foo() =
static member Test (act : Action) = act.Invoke()
Foo.Test (fun () -> ())
I can't figure out how to force the compiler to choose between `Action` and `unit -> unit`. Two other options:
- make the `Action` overload the primary and do `Foo.Test(Action(act))`
- put the logic in a third function which is called from the public methods
But my recommendation would be a single method taking `Action`. Since the conversion is automatic, interop, in a sense, is free.
Problem
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.