Functions on an enum: UploadError.describe(error) with constructors<E>().impl({...}) #149
Loading…
Reference in a new issue
No description provided.
Delete branch "enum-impl"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Enums had no place for the functions that belong to them: describing an error, the next state after a retry. They ended up as loose functions in some module. Now they go on the enum's table, like Rust's
implblock:Why on the table and not on the values (
error.describe()): values stay plain objects, soJSON.parse,validate<T>, spreads andstructuredClonekeep everything they had. Putting methods on the values would need a prototype on each value, and a value from any of those would silently lack its methods while the type still claims them. The build can't safely rewriteerror.describe()into the static call either, because it has no types for the receiver. The benchmark added here (npm run bench:enum-methods) also measured the cost: methods on the values were 1.3–3× slower to call once an enum has more than about four variants, amatchelsewhere up to 1.7× slower, andJSON.stringify1.5× slower on Node.No state on the table. Members are functions or constants (strings, numbers, booleans, bigints). An object, array or
Mapdoesn't compile, because it would be one global shared by every value. State that belongs to a value goes in its variant (Retrying: { attempt }). The table refuses writes.Build: the build now reads the member names from
.impl({...})and leaves those calls alone; before this, anyTable.x(...)call was rewritten into{ tag: "x", ... }. When the names can't be listed (.impl(members)), only a declared variant is rewritten.implandthenare never variants.Also changed: a plain
constructors<E>()table now refuses writes too (before, a write went into its hidden target and was never read). This is in the CHANGELOG under Changed.Docs:
throw-to-resultstep: an error class's methods become.implmembers;Tests (fast profile):
cargo nextest run --workspace415/415,npm run test:std458/458,npm run typecheck(including the emittedenum.check.ts),node scripts/docs-typecheck.mjs(1,118 examples),npm run test:soundness(escapes unchanged: 16 of 110).Not wired into
bench:check: the bench spawns a process per case and takes minutes, so it's a manual benchmark likebench:loops.Created with Claude Opus 5.5 in Claude Code.
`UploadError.describe(error)`, like Rust's `impl UploadError { ... }`. Members are functions or constants (strings, numbers, booleans, bigints): objects, arrays and maps don't compile, since state on the table would be one global. A member named like a variant, `impl` or `then` doesn't compile either. Values stay plain objects, and the table is read-only. The build reads the member names from `.impl({...})` and leaves those calls alone (before, any `Table.x(...)` was rewritten into `{ tag: "x", ... }`). Where it can't list them (`.impl(members)`), only a declared variant is rewritten. packages/std/bench/enum-methods.bench.ts measures why the functions live on the table and not on the values: methods on a prototype were 1.3-3x slower to call once an enum has more than about four variants, and 1.5x slower to JSON.stringify on Node. Co-Authored-By: Claude Opus 5.5 <[email protected]>3f78ad86a9bad5219357