简体   繁体   中英

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

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM