C gives you enums and unions, and the pair almost expresses a variant. Almost is where the bugs live: nothing forces a program to check the tag before reading the union, so a mismatched read returns nonsense without a complaint. Type-C closes that hole from inside the language, with no new compiler required.
The trending query Show HN: Type-C – variants, options, generics and pattern matching for C reached the Hacker News front page in late November 2026. The appeal is structural: C is about 50 years old, still runs a large share of the world's systems, and this project shows how far a library can push the language without touching its toolchain.
Sum types delete a class of bugs
A sum type, often called a variant, lets a value be exactly one of several labeled cases. A network result is a connected socket, a timeout, or a refusal, and the type records which one it is. Plain C approximates this with an enum beside a union, and nothing complains when the two drift apart.
With a variant, the case label and the payload travel together, so the invalid states a plain enum-plus-union allows have no syntax. Reading payload bytes stored for a different case, the classic silent corruption in C, becomes a construct the compiler rejects instead of a bug that ships.
Pattern matching is structured dispatch
Pattern matching replaces nested switch-and-check code with one construct: match the case and bind its fields in the same place. The handler body sees exactly the fields that case carries, so there is no second lookup to get wrong.
The shape pays off as cases multiply. A parser with a dozen result cases flattens into a list of small handlers, and adding a case surfaces the missing handler through the generated checks. Dispatch, field access, and exhaustiveness stop being three separate reviews and become one readable block.
Drop in your PDF - it doesn't leave your device.
Every miss becomes a red node on your map.
Free plan: 21 nodes, no card, no countdown.
How a library teaches an old language new types
Library-level type systems in C work through macros and designated initializers. The macro layer writes the boilerplate: tag enums, union layouts, constructor functions, and match scaffolding. Designated initializers, part of standard C since C99, let generated code fill structure fields by name, which keeps generation stable across field reordering.
The trade is compile-time cleanliness for zero runtime cost. Preprocessor errors read worse than compiler errors, and a debugger shows generated names. What you get back is plain C output with no extra instructions at run time, which is the currency that matters in kernels, embedded targets, and hot paths.
| Concern | Enum beside union | Type-C variant |
|---|---|---|
| Case checking | Manual, by convention | Generated match scaffolding |
| Wrong-case payload read | Compiles, corrupts at run time | Blocked by the type layout |
| Missing handler | Silent default path | Warning or error from generated code |
| Runtime overhead | None | None |
Features are conventions until something enforces them
The general lesson reaches past C. Many conveniences that feel like language features are discipline plus code generation: result types, null-safety wrappers, and tagged errors all exist as libraries in languages that lack them natively. The runtime never changed; the convention became mechanical.
Type-C's contribution is exactly that mechanization for C's weakest spot. Once the generated match refuses to forget a case, the discipline holds on deadline week as well as in code review, and the invalid states stay impossible.
Key takeaways
- Sum types let a value be one of several labeled cases, and the label travels with the payload.
- Plain enums beside unions permit invalid states; variants remove the syntax for them.
- Pattern matching is structured dispatch: match the case and bind its fields in one construct.
- Library type systems in C are macros and designated initializers trading compile-time cleanliness for zero runtime cost.
- Language features are conventions until something enforces them, and code generation is how a library enforces.
Quick answers
What is Type-C?
A library that adds variants, options, generics, and pattern matching to C through macros and generated code.
What is a sum type?
A value that is exactly one of several labeled cases, with the label and the payload kept together.
Why is this better than an enum beside a union?
Nothing in plain C forces the tag to match the payload, and variants make mismatched reads unrepresentable.
Does it cost runtime performance?
No. The output is plain C with no extra runtime instructions; the price is messier compile-time diagnostics.
Do I need a new compiler or toolchain?
No. The approach works inside ordinary C builds using the preprocessor and standard C features.
One thing to do tonight
Find one enum-plus-union in a C project you maintain and list every place that reads the payload after checking the tag. Any reader that skips the check is the exact bug class variants delete, and now you know what fixing it looks like.