hn.today

Show HN: Type-C – variants, options, generics and pattern matching for C

github.com43 points20 comments
Screenshot of Show HN: Type-C – variants, options, generics and pattern matching for C

Type-C is an early language extension for C11 that aims to add type-safety features without replacing C’s toolchain or programming model. Its compiler accepts C-like source, translates added constructs into readable C, and passes the result to GCC, Clang, or another C compiler. Existing C11 files remain valid, so teams can adopt it one file at a time while retaining headers, pointers, build systems, and the C ABI. The implemented examples work today, but some designed features are not yet built.

The central example uses tagged variants and exhaustive pattern matching to represent arithmetic expressions and return division-by-zero or overflow errors as values. A match must cover every variant case, and fields can only be accessed through a matching case, preventing wrong-member reads and making newly added cases trigger build errors where they are unhandled. The language also provides a built-in result type and try, which propagates errors through expressions, alongside planned support for distinct types, generics, options, and deferred cleanup. Type-C adds no runtime, hidden allocation, or garbage collector; variants, matches, and error propagation compile to C structures, switches, and early returns. The project reports parsing speed comparable to hand-written C in a real-library test, with object files 2.8% larger under Clang and 11.9% larger under GCC. It emphasizes that Type-C is not memory-safe and claims only the guarantees its checks actually provide.

Read on github.com20 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.