20. Program structure: the @!multitest divider and top-level return (0.2.2, approved 2026-11-20)
Revises 6.3. A single-test program and a multitest program have the same shape: flat, unindented top-level
statements, run top to bottom. Neither needs a fn solve() wrapper.
-
20.1
@!multitestis a divider.@!marks a file-level directive: it stands on a line of its own and is in force from that line down, where@always decorates the single item below it (the same split as Rust's#![...]and#[...]).@!multitestis the one directive, and appears once per file. Everything above it runs once, before the test count is read: sieves,Combtables, anything shared. Everything below it runs once per test case. The counttis read automatically at the divider (@!multitest(until_eof)reads cases until the input ends instead).let sv = Sieve(1e6) // above: built once @!multitest let n: 2..=1e6 = read() // below: once per test case print(sv.is_prime(n))Delete the divider and move the sieve down and what is left is the single-test program for the same problem. That symmetry is the point: turning a solution into a multitest one is adding a line and lifting the shared setup above it, never re-indenting.
-
20.2 Each test case is a fresh scope. Every
let,var,domainandfnbelow the divider is created anew for each case and gone at its end, so nothing leaks from one case to the next. A@memo fnbelow the divider belongs to its case: its cache starts empty every time. State that must outlive a case is declared above the divider, where it is visible to (and, for avar, writable from) every case.@!multitest let n: 1..=2e5 = read() domain V = 1..=n let a: [V]Int = read() @memo fn best(i: V) -> Int: // cache keyed by this case's V, emptied for the next case if let j = i.next: return max(a[i], a[i] + best(j)) a[i] print(best(V.first))
-
20.3 Top-level
return. Areturnoutside everyfntakes no value and ends the current unit of work, from any depth of loops: the whole program in a single-test file (and above the divider), the current test case below the divider. Output printed so far is kept and flushed. This is the early exit thatfn solve()wrappers existed to provide.let n: 0..=2e5 = read() // single-test: `return` ends the program if n == 0: print("empty") return@!multitest let n: 0..=2e5 = read() // multitest: `return` ends this case, the next one starts if n == 0: print("empty") returnThe two bodies are identical.
return xat top level is an error (there is no caller to receivex).
-
20.4
@specand@checkersee the same inputs in both modes. They read the top-level inputlets by name. In a single-test file those are the program's inputs; below the divider they are the current test case's inputs, and the@spec/@checkerruns once per case. The declaration is written the same way in both files.@!multitest let n: 1..=5 = read() print(n * n) @checker fn check(): let x: Int = read_output() assert x == n * n // `n` is this case's `n`
- 20.5 What is not kept.
@multitest fn solve():is no longer part of the language as taught: the divider covers precomputation (the one reason the wrapper existed) and top-levelreturncovers early exit. A second@!multitestin one file is an error, as is any other@!nameand a@!multitestthat is not at top level. A bare@multitestas a divider is the deprecated spelling of@!multitest. Problems whose case loop has another shape (a count per group, a sentinel) still write a plainfor _ in 0..tests:orread_untilloop.
- 20.6 Prototype status.
proto/cpcandproto/cpiimplement 20.1 to 20.3 and 20.5's errors;crates/cpl-cli/tests/cases/program/holds the cases, run by both. The prototype still compiles the older@multitest fn solve():wrapper and the bare@multitestdivider, so existing ports keep building until they are migrated; both should then become errors that point to@!multitest.@specand@checkerare not implemented in the prototype, so 20.4 is specified here and not yet tested.