F # Define a function to act as a .net delegate. Act

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 the following:

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

but this causes an error:

Error 102 This expression should have been of type Action, but there is type 'a → unit

How do I execute the "f" function to satisfy the compiler?

+5
source share
1 answer

If you pass the anonymous function fun a -> ... method or constructor that expects System.Action<...> or System.Func<...> , then it will be automatically converted; in any other case, you need to explicitly convert it as pointed out by @Funk.

 let f = System.Action<WorkflowApplicationCompletedEventArgs>(fun e -> // body ) myInst.Completed <- f // Another solution: let f (e: WorkflowApplicationCompletedEventArgs) = // body myInst.Completed <- System.Action<_>(f) 
+4
source

Source: https://habr.com/ru/post/1265572/


All Articles