Show HN: Type-C – variants, options, generics and pattern matching for C
Type-C is a C11 compiler extension that adds algebraic data types, pattern matching, generics, and error handling with try and defer, transpiling directly to standard C.
read it It offers a deeply thought-out approach to modernizing C codebases incrementally without runtime overhead or ABI breakage.
summary
Type-C is a C11 and C17 superset implemented as a source-to-source translator that compiles down to readable standard C. It introduces features typically missing in C, including closed sum types, exhaustive pattern matching, distinct types, generics, and try-defer syntax for safe resource management. Developers can adopt it file-by-file by renaming files to .tc and using typec as the compiler frontend. Benchmarks from a real-world TOML parser rewrite show a complete elimination of memory leaks and crashes under fault injection.
key points
- Transpiles .tc files into readable standard C, retaining full compatibility with existing C toolchains, debuggers, and linkers without adding a runtime library.
- Enforces exhaustive pattern matching on variants and prevents invalid member access or unhandled result types at compile time.
- Provides Rust-style try and defer keywords for deterministic resource cleanup and error propagation across multiple exit paths.
who should read it
Systems programmers and C developers seeking modern safety features like pattern matching and error handling without abandoning legacy C codebases.
discussion warm
consensus
Commenters are receptive to the practical utility of incrementally modernizing C, especially after the author shared compelling fault-injection benchmarks showing zero memory leaks or crashes in a parser rewrite.
pushback
A few participants questioned the choice of extending C rather than adopting established alternatives like C++, Rust, or other systems languages.
Realized that the post is a bit light on numbers, so here're some.
I rewrote the parser of tomlc99 (2k lines) in Type-C as a test. I used result/try/defer for the error paths and left everything else unchanged, linking against the same C programs and tests.
Doing fault injection with one failed allocation per run over ~1500 runs: the original C went wrong in 34 (25 leaks, 9 use-after-free); the rewrite went wrong never, sanitizers clean.
With original toml-spec-tests files: the original leaked in 1002 of them and crashed in 9; the rewrite did neither.
This is an example of how the code …
It provides concrete fault-injection metrics demonstrating dramatic reliability improvements over standard C on a real-world codebase.
Generated by an LLM from the article and thread. Check the sources before relying on it.