简体   繁体   中英

Implementing delegate interface in F#

I would like to implement the following C# interface in F#

interface IDoSomething
{  
   void Process(foo input); 
   event Action<bar> Result; 
} 

However I am failing at the event/ delegate part:

type doSomething() =
    let result = new Event<System.Action<bar>>()    
    interface IDoSomeThing with
        [<CLIEvent>]
        member x.Result = result.Publish 
          // This expression was expected to have type
          // Handler<System.Action<foobar>>    
          //  but here has type
          // System.Action<foobar>  

Any idea how this is done?

Solution

type doSomething() =
    let e =  DelegateEvent<_>()  
    let result s = e.Trigger ([| box s |]) 

    let calc input = 
        input 
        |> (...) // The real processing happens here 
        |> result

    interface IDoSomething with
        member x.Process input = calc input
        [<CLIEvent>]
        member x.Result = e.Publish

Since your interface defines a non-standard event type you'll have to use DelegateEvent<_> instead of (the more common) Event<_,_> .

type DoSomething() =
    let result = DelegateEvent<_>()
    interface IDoSomething with
        member x.Process(input: Foo) = ()
        [<CLIEvent>]
        member x.Result = result.Publish

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