What is the F # equivalent of C # "public event"

I am trying to translate the F # code from this article http://www.codeproject.com/Articles/35532/C-COM-Object-for-Use-In-JavaScript-HTML-Including

I came across the following lines:

public delegate void MyFirstEventHandler(string args); public event MyFirstEventHandler MyFirstEvent; 

I tried translating this to F # as:

 type MyFirstEventHandler = delegate of string -> unit type MyFsComComponent () = let my_event = new Event<MyFirstEventHandler,string> () [<CLIEvent>] member x.MyFirstEvent = my_event.Publish 

And I get: "MyFirstEventHandler" has a non-standard delegate

I can compile with:

 type MyFirstEventHandler = delegate of obj*string -> unit 

But this is not what COM management needs.

This issue was also raised in the C # section for class F # transition - the "public event" still had no solution.

solvable. Thanks Leaf Garland

 type MyFirstEventHandler = delegate of string -> unit type MyFsComComponent () = let my_event = new DelegateEvent<MyFirstEventHandler> () [<CLIEvent>] member x.MyFirstEvent = my_event.Publish 

does the trick.

+4
source share
1 answer

The F # Event class expects argument types to be used as general parameters, not a delegate type. He also expects the delegate to be a standard type (e.g. something like obj*eventargs->unit ). For custom delegates, use DelegateEvent.

 type MyFirstEventHandler = delegate of string -> unit type MyFsComComponent () = let my_event = new DelegateEvent<MyFirstEventHandler>() [<CLIEvent>] member x.MyFirstEvent = my_event.Publish 
+5
source

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


All Articles