Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Define function to act as delegate to .net Action

Tags:

delegates

f#

in System.Activities.WorkflowApplication there is a delegate property:

        public Action<WorkflowApplicationCompletedEventArgs> Completed { get; set; }

In my program so far, I have a variable that is an instance of this class

I want to define an F# function to set that:

let f (e: WorkflowApplicationCompletedEventArgs) = 
    // body
myInst.Completed <- f

but this produces the error:

Error 102 This expression was expected to have type Action but here has type 'a -> unit

how do I complete function "f" to satisfy the compiler?

like image 502
user1443098 Avatar asked Mar 16 '17 18:03

user1443098


1 Answers

If you pass an anonymous function fun a -> ... to a method or a constructor that expects a System.Action<...> or a System.Func<...>, then it is automatically converted; in any other case, you need to convert it explicitly like @Funk indicated.

let f = System.Action<WorkflowApplicationCompletedEventArgs>(fun e ->
    // body
)
myInst.Completed <- f

// Another solution:

let f (e: WorkflowApplicationCompletedEventArgs) = 
    // body
myInst.Completed <- System.Action<_>(f)
like image 156
Tarmil Avatar answered Oct 04 '22 02:10

Tarmil