Skip to content

26 · Algebraic Effects

Typed handler overrides

A handler with operation selectors replaces only those operations, keeping the remaining effect coverage and answer type. Selector signatures and bodies are checked against the actual effect. Composition preserves the handler frame and lexical abort semantics.

On this page

A handler with operation selectors replaces only those operations, keeping the remaining effect coverage and answer type. Selector signatures and bodies are checked against the actual effect. Composition preserves the handler frame and lexical abort semantics.

Complete example

Typed handler overrides.

typed-handler-overrides.zolo
Playground
// Composition preserves base evaluation, closures and lexical cleanup.

effect Clock { fn now() -> int; fn zone() -> str }
effect Store { fn put(value: int) -> int }
var calls = 0
var builds = 0
var events = ""
let fixed_time = 42
let __zolo_frame_id_0 = 0
let __zolo_handler_override_compose_0 = 0
let real_world: handler<Clock + Store, int> = handler {
  Clock::now() => resume(1),
  Clock::zone() => resume("UTC"),
  Store::put(value) => resume(value + 1),
}
fn choose(value: handler<Clock + Store, int>) -> handler<Clock + Store, int> { value }
// Forwarding an opaque handler parameter does not establish composition origin.

let test_world = ({ builds += 1; real_world }) with {
  Clock::now() => { calls += 1; defer { events += "defer/" }; fixed_time + __zolo_frame_id_0 + __zolo_handler_override_compose_0 },
}
print(with test_world { perform Clock::now() + perform Store::put(2) })
print(with test_world { perform Clock::zone(); 7 })
print(with test_world { perform Clock::now() })
print(calls)
print(events)
print(builds)
let twice = test_world with { Store::put(value: int) => value + 10 }
print(with twice { perform Store::put(2) })
fn lexical(value: handler<Clock + Store, int>) -> int {
  defer { events += "caller/" }
  with value { return 9 }
  0
}
print(lexical(twice))
print(events)

Rules and behavior

Handlers and resource scopes · Documentation

From a checkout of the repository:

zolo examples/features/typed-handler-overrides.zolo

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br