Flows and Subflows

In Koru, flows are used to define the sequence of events and their possible outcomes. Flows can be nested to create complex event sequences.

Flows

~import "$std/io"

const file = ~std.io.file:open(file: "example.txt")
| file f |> std.io:println(f.name) |> file { file: f }
| error e |> std.io:eprintln(e.reason) |> error { msg: "Failed to open file" }

Proc Implementation

A proc provides the implementation for an event:

~proc greet {
    if (name.len == 0) {
        return .{ .@"error" = .{ .reason = "Name cannot be empty" } };
    }
    const msg = try std.fmt.allocPrint(allocator, "Hello, {s}!", .{name});
    return .{ .greeting = .{ .message = msg } };
}

Invoking Events

Events are invoked with flows:

~greet(name: "World")
| greeting g |> std.io:println(g.message)
| error e |> std.io:eprintln(e.reason)