Search code examples
f#objecttypesinference

F# Type Inference and System.Object


I have a problem getting the following code to work.

open System
open System.ComponentModel
open System.Threading

let triggerEvent (state : Object) = Console.Write("Do Something.")

let asyncContext = AsyncOperationManager.CreateOperation(null)
asyncContext.PostOperationCompleted(triggerEvent, null)

I get an error, that triggerEvent is of type 'a -> unit instead of SendOrPostCallback. SendOrPostCallback is of type Object -> unit. I am wondering why triggerEvent is of type 'a -> unit instead of Object -> unit. I explicitly declared state as Object and still it is 'a.

Any suggestions? Thank you.


Solution

  • I'm not an expert on threading, but if PostOperationCompleted expects a SendOrPostCallback, try wrapping your triggerEvent like this: replace

    asyncContext.PostOperationCompleted(triggerEvent, null)
    

    by

    asyncContext.PostOperationCompleted(new SendOrPostCallback(triggerEvent), null)