LEARN VOID / DATA & BEHAVIOR

Events

Connect objects through ordered notifications.

events.voidDownload source ↓
struct sensor:
    event measured: func(i32)

func (source sensor) publish(reading: i32):
    source.measured.emit(reading)

struct display:
    latest_reading: i32 = 0

func (screen display) update(reading: i32):
    screen.latest_reading = reading

func entry(reading: i32) -> i32:
    var source = sensor()
    var screen = display()
    source.measured += screen.update
    source.publish(reading)
    return screen.latest_reading
Input 5Expected result 5

Declare a notification

The sensor exposes a measured event carrying one integer reading. Its publish method emits that reading from the object that owns the event.

Connect a receiver

source.measured += screen.update subscribes the display’s method. When the sensor publishes input 5, the display stores it as its latest reading.

Read the updated state

Delivery is synchronous, so the display has been updated before publish returns. The program returns 5. Multiple subscribers are notified in subscription order; events retain strong references to subscribers.