As you already mentioned, you need to create an instance of delegate:
let ch = new MouseButtonEventHandler(fun obj args ->
printfn "Click!")
Then you can use ch
as an argument to AddHandler
or RemoveHanlder
. The reason is, that F# function value isn't actually represented as any delegate type. It has it's own type, which isn't a delegate (see also another SO discussion). You can see the difference if you look at the types in F# interactive (or VS IntelliSense):
> (fun (o:obj) (a:MouseButtonEventArgs) -> printfn "Click!")
val it : obj -> MouseButtonEventArgs -> unit = (...)
> new MouseButtonEventHandler(fun o a -> printfn "Click!")
val it : MouseButtonEventHandler = (...)
What may be confusing is that F# also allows implicit conversion from lambda function value to a compatible delegate type when calling a method, however this works only when you give a lambda function directly as the argument (I think):
fe.MouseLeftButtonDown.AddHandler(fun obj args ->
printfn "Click!")
Another reason why you still need delegates in F# is that delegates have a well-defined comparison semantics (they are compared as any other reference types). For functions, the F# specification doesn't say when they are equal.