48 points · by stass · 2d ago · 22 comments
Of course there is Swift, Ocaml or Rust for situations like that, but then you are either forced to convert the entire codebase, or add a runtime library or go through the pain of bridging C to the new language. Sometimes there are no compilers for the target architecture too when you are working on embedded stuff.
This is where Type-C comes in. It works as a preprocessor like TypeScript and calls the original C driver behind the scenes. Your code stays normal C for the most part, so you can predict how the code will be laid out. TypeC is a full superset of C11, so any C11 code compiles as is.
Want to use TypeC just for one file? Rename to .tc and use the features you want, the rest of the codebase stays the same. This works for kernel or baremetal code too as there is no runtime or extra libararies.
There are examples of TypeC code as well as real-world tests of converting existing code to TypeC on the GitHub page.
This is how the code looks like for an expression evaluator: type expr = | Num(int value) | Add(const expr l, const expr r) | Mul(const expr l, const expr r) | Div(const expr l, const expr r);
static result(int, eval_error)
eval(const expr *e)
{
int d;
match (*e) {
case Num(value):
return (Ok(value));
case Add(l, r):
return (fit((long long)try eval(l) + try eval(r)));
case Mul(l, r):
return (fit((long long)try eval(l) * try eval(r)));
case Div(l, r):
d = try eval(r);
if (d == 0)
return (Err(DivByZero));
return (fit((long long)try eval(l) / d));
}
}
% typec -std=c11 -Wall -Wextra eval.tc
$ ./a.out
14
division by zero
Would love to hear your thoughts and feedback!Why C11? We're in 2026, we should target C23 at least, with a forward look at C2y.
Why the keyword "type"? Can't the same functionality be added within a typedef statement? Is there a reason why "|" can't be recognized wherever a type is expected?
And finally, the name "Type-C"... Isn't it already closely associated with USB connectors?
Mostly compiler support. I wanted for it to work with a wide range of compilers, so didn't want to introduce a dependency on C23 or beyond. Having said that, I expect Type-C to any C23 code as well, but I haven't systematically tested that.
Basically it's designed to support any somewhat recent random ass compiler as a backend, like GCC for SH4.
> Why the keyword "type"? Can't the same functionality be added within a typedef statement?
This would be tricky. In C typedef creates an alias for an existing type, not a new type, so overloading typedef would make it mean two different things depending on the context. A more technical reason is that typedef puts the new name last where is Type-C needs it come first so it can be used in recursive types like e.g. "type expr = | Num(long v) | Add(expr l, expr r)". This is the reason C itself needs forward declaration for structs for recursive types.
A new keyword, type, costs nothing as Type-C only recognizes it at the beginning of declarations which is not a valid C anyway.
> Is there a reason why "|" can't be recognized wherever a type is expected?
It can, but that would be challenging and ROI is unclear. Right now variant types are nominal, so the same "A(int)|B" type in two places will be treated as two separate types. Making variant types structural, especially across multiple .tc modules, is... not even sure it's even possible to build the way Type-C is built. C code Type-C generates also needs a name for the C type and currently that comes from the declared Type-C type. Anonymous types will need a stable name across interfaces, so typec compiler will need to parse the entire codebase or become a standalone compiler instead of emitting C.
> And finally, the name "Type-C"... Isn't it already closely associated with USB connectors?
It came from TypeScript as it's a TypeScript idea applied to C. And having it sound like a Type-C connector seemed like a fun pun.
Fair enough. As long as it's not confused by literals like 1'000 and 0b0110, attributes in the form [[...]], new keywords as "constexpr", or a subscript qualified with "cost" or "static" in the parameters list.
> typedef creates an alias for an existing type, not a new type
Exactly. C is based on the concept that you can express (nearly) every type when you need it, even without giving it a name. In some cases you might need to create a new tag, but not a new type. Why your sum types are different? Why don't you handle them like their "sibling" structs, unions and enums? Forget the keyword "type", define the keyword "variant", rewrite your example like this:
variant eval_error {
DivByZero,
Overflow
};
You can typedef it, but it's not mandatory: typedef variant eval_error EE;
To allow recursive definition, use forward declaration as all the rest of C: variant expr;
variant expr {
Num(int value),
Add(const variant expr *l, const variant expr *r),
Mul(const variant expr *l, const variant expr *r),
Div(const variant expr *l, const variant expr *r)
};
It's more verbose but consistent with C syntax. And it allows one-shot declarations without naming a new type like: variant {Ok(float), Inf} atan(float);
Not saying it's better. Just more familiar.> Anonymous types will need a stable name across interfaces,
I don't think so. If anonymous structs and unions don't have this property, why should variants? (But you might consider how C23 handles structs with the same tag!)
Presumably C++ is the first thing you should mention.
On a serious note, it's a fair point: it does have std::variant, std::visit and std::expected in C++23, but the do force you to embrace C++ verboseness: e.g. you have to write a per-case visitor for std::visit compile-time check. It also still lacks exhaustive matching, distinct types, and has a different ABI from C.
I felt like Swift/OCaml were closer in spirit to what I was trying to achieve in Type-C.
I wanted to do the same thing to python. So wrote a parser for a variant of python.
It's spy-ast on pypi
I picked OCaml vs vlang mostly because I'm more familiar with it and it's so well suited for parsers/compilers with its ADTs and pattern matching -- ironically the same features I wanted to add to C. It has a great ecosystem of existing tools and packages as well and cross-platform support, which is good for portability.
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 looks like for an expression evaluator: type expr = | Num(int value) | Add(const expr l, const expr r) | Mul(const expr l, const expr r) | Div(const expr l, const expr r);
static result(int, eval_error)
eval(const expr *e)
{
int d;
match (*e) {
case Num(value):
return (Ok(value));
case Add(l, r):
return (fit((long long)try eval(l) + try eval(r)));
…
Happy to answer any questions!Memory safety, race conditions, unhandled integer overflows. Look at all newly-discovered vulnerabilities, they almost all caused by these issues.
> Some of the C pains are hard to solve without breaking the language.
That's why it's necessary to do such breaking change, to stop using C and to switch to better languages having no issues mentioned above.
This is not always possible, however, especially for legacy/existing projects, and that's the hole Type-C plugs. Imagine a legacy embedded FreeRTOS or RTEMS or Zephyr codebase with existing tooling where you want to introduce a little be of patter matching convenience and type safety? With Type-C you would just rename one of the files to .tc, use whatever features you need, add a new .tc rule to the Makefile and that's it. Everything else will work as it did, with the same compiler, tooling and so on. No new compiler, runtime or having to convince coworkers to switch to a new language.
For some still widely used architectures, like SH4, there isn't even Clang compiler yet, let alone Rust or Ocaml. So you need C.
This is the use case Type-C targets.