✓
Passing This code compiles and runs correctly.
Code
~import app/connection
~app/connection:create()
| created c1 |> app/connection:connect(conn: c1)
| connected c2 |> app/connection:resume(conn: c2) // active -> resume accepts it!
| resumed c3 |> app/connection:close(conn: c3)
~app/connection:create()
| created c1 |> app/connection:connect(conn: c1)
| connected c2 |> app/connection:pause(conn: c2)
| paused c3 |> app/connection:resume(conn: c3) // paused -> resume accepts it!
| resumed c4 |> app/connection:close(conn: c4)
~app/connection:create()
| created c1 |> app/connection:connect(conn: c1)
| connected c2 |> app/connection:close(conn: c2) // active -> close accepts it!
~app/connection:create()
| created c1 |> app/connection:connect(conn: c1)
| connected c2 |> app/connection:pause(conn: c2)
| paused c3 |> app/connection:close(conn: c3) // paused -> close accepts it!
Actual
Creating connection
Connecting: 1
Resuming/continuing: 1
Closing: 1
Creating connection
Connecting: 1
Pausing: 1
Resuming/continuing: 1
Closing: 1
Creating connection
Connecting: 1
Closing: 1
Creating connection
Connecting: 1
Pausing: 1
Closing: 1
Expected output
Creating connection
Connecting: 1
Resuming/continuing: 1
Closing: 1
Creating connection
Connecting: 1
Pausing: 1
Resuming/continuing: 1
Closing: 1
Creating connection
Connecting: 1
Closing: 1
Creating connection
Connecting: 1
Pausing: 1
Closing: 1
Imported Files
// Library module: connection
// Demonstrates phantom state unions - events that accept multiple states
const std = @import("std");
// Connection type with phantom states
const Connection = struct {
id: i32,
};
// Create a new connection - starts in 'idle' state
~pub event create {}
| created *Connection<idle>
~proc create|zig {
std.debug.print("Creating connection\n", .{});
const allocator = std.heap.page_allocator;
const c = allocator.create(Connection) catch unreachable;
c.* = Connection{ .id = 1 };
return .{ .created = c };
}
// Connect - transitions from idle to active
~pub event connect { conn: *Connection<idle> }
| connected *Connection<active>
~proc connect|zig {
std.debug.print("Connecting: {d}\n", .{conn.id});
return .{ .connected = conn };
}
// Pause - transitions from active to paused
~pub event pause { conn: *Connection<active> }
| paused *Connection<paused>
~proc pause|zig {
std.debug.print("Pausing: {d}\n", .{conn.id});
return .{ .paused = conn };
}
// Resume - accepts EITHER paused OR active (union!) - always returns active
// This demonstrates the core union feature: flexible input acceptance
~pub event resume { conn: *Connection<paused|active> }
| resumed *Connection<active>
~proc resume|zig {
std.debug.print("Resuming/continuing: {d}\n", .{conn.id});
return .{ .resumed = conn };
}
// Close - accepts active OR paused (union!) - cleans up connection
~pub event close { conn: *Connection<active|paused> }
~proc close|zig {
std.debug.print("Closing: {d}\n", .{conn.id});
}
Test Configuration
MUST_RUN