· via Hacker News – Front Page (native)
Type-C extends C11 with variants, generics and pattern matching, compiled to readable C
Type-C extends C11 with closed variants, exhaustive matching, generics and errors as values, then compiles everything into readable C for existing GCC and Clang toolchains.
A project called Type-C, which reached the front page of Hacker News, extends C11 with the type-level constructs the language has never had: closed variants, exhaustive pattern matching, distinct types for identifiers and units, generics, and errors expressed as values. Its compiler, typec, accepts the command line of cc, translates the extended source into readable C, and hands that C to an ordinary C compiler, so programs build with existing toolchains, headers and debuggers.
A C file is already a Type-C file
According to the project's README, Type-C is a strict extension rather than a new language: any C11 file keeps its meaning, so a codebase adopts it one file at a time. A developer renames a single .c file to .tc, adds a build rule, and uses the new types where they help; the other files do not change. The compiler passes its generated C to cc by default and honours the usual compiler options.
typec can be installed through Homebrew on macOS and Linux (brew install stass/tap/typec), and Homebrew builds it from release source with OCaml and dune. The project is candid about its maturity: every example in the README works today, while the rest of the language is designed but not yet built.
Variants and exhaustive matching
The core pitch is fixing tagged unions. The README opens with a textbook bug: an area() function over a shape union that reads the circle member inside the RECT branch and quietly forgets TRIANGLE behind a default that returns zero. Compiled with cc -std=c11 -Wall -Wextra, it passes without a word.
In Type-C, the same type is a closed sum: a value is exactly one of the cases and holds only that case's fields. Fields are reached by naming them in a case, so the wrong-member read cannot be written at all. A match must cover every constructor; add a new case to the variant and the build stops at every match that misses it, with a match-coverage error naming the missing constructor. The README notes that C's warnings flag a forgotten enumerator only in narrow circumstances, and that no warning flag it tested catches the wrong-member read. A single case can also be tested with if (case ...), which binds that case's fields; outside match and these constructs, code cannot name a field.
Errors as values, no runtime
Every Type-C file has result(int, e), a built-in variant holding Ok of a value or Err of an error. try stands inside an expression: try eval(l) yields the value of an Ok, or returns the Err from the enclosing function immediately, with operands running first and left to right. The design also covers defer-style cleanups.
Nothing is bolted on at run time. The README states that a variant becomes a struct of a tag and a union of the case fields, a match becomes a switch on the tag, and try becomes an early return. There is no hidden allocation, garbage collector, exception machinery or runtime library, and nothing is added to the link.
Measured cost, stated limits
On a real library, the README reports, the Type-C rewrite parses as fast as a hand-written C twin within measurement noise, with object code 2.8% larger under Clang and 11.9% under GCC. Every example on the page is checked by tests on both compilers, and each guarantee is stated alongside what it depends on.
The limits are deliberate. Type-C adds no module system, build tool or package manager of its own; its interfaces are headers that #include reads, and a program keeps the translation units and build it already has. And unlike safe Rust or OCaml, Type-C is not memory safe: the aim is ruling out specific classes of mistake in code that stays C.
Why it matters
The usual route to stronger guarantees is rewriting in Rust, Zig or OCaml, which brings their checks only where code is actually rewritten and forces bindings or stubs at the boundary. Type-C's checks begin in the first file you rename. For the large body of C that will never be ported anywhere, a compiler that rejects silent union-member mix-ups and non-exhaustive switches, at essentially no measured parse-time cost and a few percent of object size, is a trade maintainers can make file by file while keeping their compiler, ABI and debugger.
- #c
- #compilers
- #programming-languages
- #pattern-matching
- #open-source