Skip to main content

Hew 0.6.0: concurrent work with ordinary calls

· 4 min read
v0.6.0

A file tool often has several independent jobs: read each input, calculate a result, then report the results together. You can write each job as an ordinary Hew function and choose where to start it concurrently.

Hew 0.6.0 brings that model through native compilation, typed failures and resource cleanup. Here’s a small tool that counts the bytes in two files.

Start with the work

Save this as sizes.hew:

import std.fs;
 
fn byte_count(path: string) -> i64 fails fs.IoError {
    let contents = fs.read_bytes(path)?;
    contents.len()
}
 
fn main() fails fs.IoError {
    let results = await fork [
        byte_count("first.txt"),
        byte_count("second.txt"),
    ];
    for result in results { println(result?); }
}

byte_count reads a file and returns its length. The declaration fails fs.IoError gives the function a typed failure, and ? propagates a read error to its caller. This example reads each whole file into memory, so its memory use grows with the size of the inputs.

Create two small inputs and run it:

printf 'Hew\n' > first.txt
printf 'hello\n' > second.txt
hew run sizes.hew

The output is:

4
6

The batch starts both calls and collects their results in input order. Each call can fail independently, so results contains a Result for each input. The loop uses ? on each result before printing its byte count.

Choose where to wait

An ordinary call waits for its result. fork starts independent work and returns a task; await joins that task. Writing await fork [...] places those two steps together, as the example does.

You can also keep a task handle while doing other work, then await it later. A tuple batch accepts jobs with different result types.

For a “first result wins” operation, race [...] produces a task too. Before its result becomes available, it cancels and drains the losing work, including cleanup. select waits for readiness across its branches; task handles that aren’t selected keep running. The language guide covers these forms and their uses.

Let resources follow the work

Concurrent jobs often own more than a byte count. A network request holds a connection; a process job has output pipes and a child to reap. When a task ends or is cancelled, its resources need to close even if it was suspended.

Hew carries ownership and cleanup through task handles, callbacks, collections and generic calls. A nested collection update preserves the earlier value’s snapshot. Deferred cleanup also applies across suspension. defer err adds cleanup for failure propagation, faults and cancellation; returning a Result.Err as an ordinary value leaves that value for the caller to handle.

In native programs, socket, pipe and TLS operations suspend without occupying a scheduler worker. Idle scheduling waits for events and actual deadlines. Process capture drains stdout and stderr concurrently and handles cancellation, termination and reaping.

This release also fixes a listener shutdown race: an accept call starting during shutdown could miss the wakeup and leave the program hanging. It now observes that the listener has closed and finishes waiting.

Upgrade and try it

The SDK is available for Linux x86_64 and ARM64, macOS Intel and Apple silicon, Windows x86_64, and FreeBSD x86_64. Prebuilt FreeBSD tools require FreeBSD 15.1 or newer. Consult the capability matrix when targeting the sandbox.

Install 0.6.0, then run the example above:

curl -fsSL https://hew.sh/install | bash -s -- --version 0.6.0

For an existing project, follow the 0.6 migration guide. Resolve package dependencies first, run hew fmt --migrate --root ., and review the changes before checking and rebuilding. Records use type, enum variants use dotted forms, and keyed construction uses braces, including spawn Worker { id: 1 }. Replace the former blocking race { ... } with await race [...].

Keep the compiler, runtime and standard library on the same SDK version, and rebuild native integrations with it. The release notes cover the remaining changes, including typed data codecs, property tests, benchmarks and editor support.

Hew Notes

Language development, engineering articles, releases and community projects. About once a month, with an extra issue for a significant release. Written in English.

Subscribe to Hew Notes