✓
Passing This code compiles and runs correctly.
Code
~import app/handle
~app/handle:open()
| opened h1 |> app/handle:finalize(h: h1)
~app/handle:open()
| opened h1 |> app/handle:start-close(h: h1)
| closing h2 |> app/handle:finalize(h: h2)
Actual
Opening handle
Finalizing handle: 42
Opening handle
Starting close: 42
Finalizing handle: 42
Expected output
Opening handle
Finalizing handle: 42
Opening handle
Starting close: 42
Finalizing handle: 42
Imported Files
// Library module: handle
// Demonstrates phantom state unions - events that accept multiple states
const std = @import("std");
// Handle type with phantom states
const Handle = struct {
id: i32,
};
// Open a handle - produces obligation (must be finalized)
~pub event open {}
| opened *Handle<opened!>
~proc open|zig {
std.debug.print("Opening handle\n", .{});
const allocator = std.heap.page_allocator;
const h = allocator.create(Handle) catch unreachable;
h.* = Handle{ .id = 42 };
return .{ .opened = h };
}
// Start closing - explicitly discharges opened! and produces closing! obligation
~pub event start-close { h: *Handle<!opened> }
| closing *Handle<closing!>
~proc start-close|zig {
std.debug.print("Starting close: {d}\n", .{h.id});
return .{ .closing = h };
}
// Finalize - accepts EITHER opened OR closing, discharging whichever obligation exists
~pub event finalize { h: *Handle<!opened|closing> }
~proc finalize|zig {
std.debug.print("Finalizing handle: {d}\n", .{h.id});
// Clean up the handle
}
Test Configuration
MUST_RUN