Search code examples
f#system.reactiveagent

f# function not recognized as parameter


Please unwrap these type signatures to help me understand why this doesn't work. Then, if you have a solution, that would be great too.

I have this code and the agent.Post command has the signature Observer.Create<'T>(onNext: Action<'T>) : IObserver<'T>

let reservatinoRequestObserver = Observer.Create agent.Post
interface IHttpControllerActivator with

To my knowledge, this means that Observer.Create should take an Action with a single generic parameter and then return an IObserver.

Now the definition of Post is member MailboxProcessor.Post : message:'Msg ->unit

So... Post is a method, no? It is a method that takes a single parameter no? And it returns void no? So shouldn't it be a candidate for Observer.Create? Isn't that the exact specification of Action<'T>?

Well, somethings up, I get This function takes too many arguments, or is used in a context where a function is not expected: enter image description here

Help me out... I freely admit I suck at F#


Solution

  • First, agent.Post returns unit, which is a different thing from void. F# will usually convert back and forth between void and unit for you, but they are not the same thing.

    Second, F# functions do not implicitly convert to .NET delegates. But there are some ways to do it:

    You can explicitly create the delegate using its constructor:

    let o = Observer.Create (new Action<_>( agent.Post ))
    

    Lambdas are nicely wrapped too

    let o = Observer.Create (fun msg -> agent.Post msg)
    

    Use F# Rx-wrappers

    Also there are a couple of F# wrappers/interop for Rx on nuget - just have a look, I think any will do