026 nested for in if

✓ Passing This code compiles and runs correctly.

Code

// TEST: Nested ~for inside ~if with outer obligation
// STATUS: IMPLEMENTED
//
// Open file, then conditionally run a loop, close after.
// The obligation created before the if can be disposed in
// either branch (both run at most once).

~import "$std/control"
~import "$app/fs"

const should_loop = true;

~app.fs:open(path: "test.txt")
| opened f |>
    std.control:if(should_loop)
    | then |>
        std.control:for(0..3)
        | each _ |> _
        | done |>
            app.fs:close(file: f.file)
            | closed |> _
    | else |>
        app.fs:close(file: f.file)  // Still need to close if no loop
        | closed |> _

pub fn main() void {}
input.kz

Imported Files

// Library module: fs
// Defines filesystem operations with cleanup obligations

const std = @import("std");

// File type with phantom states
const File = struct {
    handle: i32,
};

// Open a file - returns opened! state (requires cleanup)
~pub event open { path: []const u8 }
| opened { file: *File[opened!] }

~proc open {
    std.debug.print("Opening file: {s}\n", .{path});
    const allocator = std.heap.page_allocator;
    const f = allocator.create(File) catch unreachable;
    f.* = File{ .handle = 42 };
    return .{ .opened = .{ .file = f } };
}

// Close a file - CONSUMES opened! state (disposes the resource)
~pub event close { file: *File[!opened] }
| closed {}

~proc close {
    std.debug.print("Closing file\n", .{});
    return .{ .closed = .{} };
}
fs.kz