Ambiguity overload resolution between Action and unit & # 8594; type units in F #

Given the following snippet:

type Foo() =
    static member Test (act : unit -> unit) = act()
    static member Test (act : Action) = Foo.Test act.Invoke

I get an error in the last line: a unique overload for the Test method cannot be determined based on type information up to this point in the program. Type annotation may be required.

Unfortunately, a type annotation (act.Invoke : unit -> unit)does not eliminate the ambiguity, and I cannot find an annotation that corrects it. I would like the version to Actionbe a wrapper around the version ->. My specific use case is the definition of a class that will be called from both F # and C #, so I want it to work from both languages.

+3
source share
2 answers

F # Action , () .

type Foo() =
    static member Test (act : Action) = act.Invoke()

Foo.Test (fun () -> ())

, Action unit -> unit. :

  • Action Foo.Test(Action(act))
  • ,

, Action. , , , .

+4

, , F # , , , F # (. 8.13.6 .)

, , , let-bound , ( ):

type Foo() = 
    static let test act : unit = act()
    static member Test act = test act
    static member Test (act : System.Action) = test act.Invoke

, , , .

+1

All Articles