Note: None of this is implemented.

Earlier, we introduced value-based closures. Value-based closures struggle when we need closures that have a specific calling signature, but whose underlying struct state may vary in size and fields. To satisfy this requirement, a closure may be passed as a virtual reference.

Let's illustrate this with a simple event-handling example:

window.whenClicked(&so |{window}| { ... })

Here we allocate a new click-handling closure using the single-owner region (ensuring it is automatically freed after being triggered and discarded). The closure's state remembers the window, which its logic uses when the window is clicked.

The whenClicked method accepts any closure whose type signature is &<so ||. In other words, it accepts closures having any underlying state, so long as the closure's function signature matches (it accepts no arguments and returns no values).

Closures allocated this way are virtual references. In the same way that closures are implicit structs, closure references take advantage of an implicit trait, one which defines the appropriate method signature for the () method.

When the event fires on a click, the event handler simply calls the closure to perform its defined logic:

handler()

_