Exploring F# Events
F# Events are a powerful mechanism that allow you to implement event-driven programming in your F# code, enabling communication and coordination between different parts of your application.
In this article, we will cover how to create F# events and how to use them. As we begin this tutorial, we are going to discuss some of the basics of event handling in F#, such as defining and raising events.
After that, we will discuss how to subscribe to events and how to handle events using anonymous functions, among other things.
Understanding F# Events
F# events notify you and/or other objects when a certain action has taken place. Events are a powerful feature that can be utilized in various scenarios, such as updating user interfaces, handling user input, or performing background processing.
F# events can be used to communicate between classes, allowing them to send and receive messages. For example, in a GUI application, events can represent actions such as a keyboard press, a click, or a mouse movement, or even system-generated events like notifications. It is important for your application to handle these events in order to respond appropriately when they occur, just like handling interrupts or communication between processes.
Objects in F# can communicate with each other through synchronous message passing, allowing them to exchange information and coordinate their actions. This can be achieved by attaching a callback function, also known as an event handler, to an event that is triggered by another function or object. When the event is triggered, the callback function is executed, allowing you to respond to the event accordingly.
F# events allows you to implement efficient communication and coordination between different parts of your application, promoting loose coupling, separation of concerns, and extensibility. By leveraging the power of events, you can enhance the functionality and responsiveness of your F# applications, making them more robust and user-friendly.
The Event Class And Module
The Control.Event<T> class helps create observable events.
In order to work with the events, it has the following instance members:
Member | Overview |
Publish | A first class value is published when an observation is made. |
Trigger | This method triggers an observation based on the parameters you provide. |
Using the Control.Event Module, you are able to manage event streams through the following functions:
Value | Overview |
add : (‘T → unit) → Event<‘Del,’T> → unit | Whenever the given event is triggered, the given function is run each time it is triggered. |
choose : (‘T → ‘U option) → IEvent<‘Del,’T> → IEvent<‘U> | In this function, you will get a new event which will happen when a selection of messages from the original event are received. With the selection function, you can take an original message and create an optional new message from it. |
filter : (‘T → bool) → IEvent<‘Del,’T> → IEvent<‘T> | In this function, we will return a new event triggered by the original event, however, the resulting event will only get triggered if the argument of the original event does not pass the given function. |
map : (‘T → ‘U) → IEvent<‘Del, ‘T> → IEvent<‘U> | This function is responsible for returning a new event that passes values that have been transformed by the given function. |
merge : IEvent<‘Del1,’T> → IEvent<‘Del2,’T> → IEvent<‘T> | This event is fired when either the input event or the output event has been fired. |
pairwise : IEvent<‘Del,’T> → IEvent<‘T * ‘T> | This function returns a new event which is triggered upon the second triggering of the input event, and any subsequent triggering as well. This is the Nth triggering of the input event, which passes the arguments from the N-1th and Nth triggers as a pair to the Nth triggering. Until the Nth triggering of the event occurs, the argument passed to the N-1th triggering will be held in a hidden internal state until the Nth triggering occurs. |
partition : (‘T → bool) → IEvent<‘Del,’T> → IEvent<‘T> * IEvent<‘T> | There will be two resulting events generated by this function. In the first instance, the event will be triggered if the application of the predicate to the event arguments returned true, and in the second instance, the event will be triggered if the predicate did not return true. |
scan : (‘U → ‘T → ‘U) → ‘U → IEvent<‘Del,’T> → IEvent<‘U> | It produces a new event by applying the given accumulating function to successive values stored in the input event in order to obtain the results of adding those successive values to the input event. The current value of a state parameter can be found in an item of internal state. Since the accumulation function does not lock the internal state, care should be taken to ensure the input IEvent is not triggered simultaneously by multiple threads. |
split : (‘T → Choice<‘U1,’U2>) → IEvent<‘Del,’T> → IEvent<‘U1> * IEvent<‘U2> | Upon applying the function to the event arguments, the function returns two events: one event for Choice1Of2, and one for Choice2Of2. |
Creating Events In F#
In order to create and use events, you need to use the Event class.
A new event can be created by using the Event constructor.
type Worker(name : string, shift : string) = let mutable _name = name; let mutable _shift = shift; let nameChanged = new Event<unit>() (* creates event *) let shiftChanged = new Event<unit>() (* creates event *) member this.Name with get() = _name and set(value) = _name <- value member this.Shift with get() = _shift and set(value) = _shift <- value
Afterwards, you require exposing the nameChanged field as a public member of the event definition in order for listeners to be able to latch on to the event when it occurs, which can be achieved by using the Publish property of the event definition:
type Worker(name : string, shift : string) = let mutable _name = name; let mutable _shift = shift; let nameChanged = new Event<unit>() // creates event let shiftChanged = new Event<unit>() // creates event member this.NameChanged = nameChanged.Publish (* exposed event handler *) member this.ShiftChanged = shiftChanged.Publish (* exposed event handler *) member this.Name with get() = _name and set(value) = _name <- value nameChanged.Trigger() // invokes event handler member this.Shift with get() = _shift and set(value) = _shift <- value shiftChanged.Trigger() // invokes event handler
After the event handlers have been created, you must add callbacks to them.
The event handler class is of type IEvent<T>, which provides several methods:
Method | Overview |
val Add : event:(‘T → unit) → unit | The event is connected to a listener function. Listeners will be invoked when events are fired. |
val AddHandler : ‘del → unit | The delegate object is used to connect a handler to the event. The RemoveHandler method can be used to remove a handler at a later time. As soon as the event is triggered, the listener will be called. |
val RemoveHandler : ‘del → unit | Deletes a listener delegate from the list of listeners for an event. |
A complete example of this can be found below.
Here is an example demonstrating how the techniques and concepts discussed above can be applied:
Example: 
The output will be:
Worker changed name! New name: Ben Worker changed name! New name: Alex -- Another handler attached to NameChanged! Worker changed shift! New shift: Morning Worker changed shift! New shift: Night -- Another handler attached to ShiftChanged!
Example Explanation
In this example, we create a type called Worker that has a mutable name and shift. We also create two events called nameChanged and shiftChanged using the Event type. These events are used to notify when a worker’s name and shift change.
As a programmer, we create an instance of the Worker type and assign it to the worker variable. We then attach a handler to the NameChanged event by calling the Add method on the event and passing a lambda expression that prints a message to the console when the name changes.
After that, we change the worker’s name by assigning a new value to the Name property. This invokes the Trigger method of the nameChanged event, which in turn invokes the attached handler and prints the message to the console.
We then attach another handler to the NameChanged event by calling the Add method again and passing a different lambda expression that prints a different message to the console when the name changes. We change the worker’s name again, and both handlers are invoked in the order that they were attached.
Next, we attach a handler to the ShiftChanged event in the same way we did with the NameChanged event. We change the worker’s shift by assigning a new value to the Shift property. This invokes the Trigger method of the shiftChanged event, which invokes the attached handler and prints the message to the console.
Finally, we attach another handler to the ShiftChanged event by calling the Add method again and passing a different lambda expression that prints a different message to the console when the shift changes. We change the shift of the worker again, and both handlers are invoked in the order that they were attached.
F# Events Benefits
F# events offer several benefits that make them a valuable tool in event-driven programming:
- F# events allow you to implement asynchronous communication between different parts of your application. This means that different components of your application can communicate with each other without blocking or waiting for each other to complete, enabling concurrent and parallel processing.
- F# events promote loose coupling between different parts of your application. Event producers (objects that raise events) and event consumers (objects that subscribe to events) are loosely coupled, meaning they do not have to know about each other’s implementation details. This promotes modularity and flexibility in your code, making it easier to modify or extend your application without affecting other parts.
- F# events allow you to separate concerns and responsibilities in your code. Event producers are responsible for raising events, and event consumers are responsible for handling events. This allows you to divide the responsibilities of your application into smaller, more manageable units, making your codebase more maintainable and easier to understand.
- F# events provide a way to extend the functionality of your application by allowing external components to subscribe to events and react to them. This makes it easy to add new features or behavior to your application without modifying the existing code, promoting extensibility and flexibility.
Conclusion
F# events are a powerful feature that allow you to implement communication and coordination between objects and classes in your applications. They provide a way to notify one or more objects when a certain action has taken place, allowing for efficient handling of user input, system-generated events, and other scenarios. By attaching callback functions to events, you can respond to events in a timely manner and enhance the functionality and responsiveness of your F# applications. Whether you’re building user interfaces, handling input, or performing background processing, events can be a valuable tool in your F# programming arsenal. So, don’t hesitate to leverage the power of events in your F# code to create more robust, extensible, and user-friendly applications.