Writing a F# Windows Forms application I discovered the Event.Filter function of F# and I’d like to learn more about it.
What I’ve done is to convert my existing code :
MyControl.MouseMove.Add( fun args ->
if (args.Button = MouseButton.Left && args.X > 10 && args.Y > 10)
then // do something
to :
MyControl.MouseMove
|> Event.filter ( fun args ->
(args.Button = MouseButtons.Left && args.X > 10 && args.Y > 10))
|> Event.add ( // do something)
I’ve found the Event.Filter solution really elegant, so I’d like to know if it is only a sort of syntactic sugar or which are the differences of how things work “under the hood”.
Looking at the source, you can see that your two code samples do almost precisely the same thing. The only additional overhead is the creation of an extra
Eventbyfilter.Not using
filterdue to performance would be an excessive optimization, in my view.