# Type-C: 为C语言注入现代类型安全特性

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

> Hacker News story 50602561 · language: zh · submitted 2026-11-22
> Web version: https://zeli.app/zh/story/50602561
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 35 · by [stass](<https://news.ycombinator.com/user?id=stass>) · 💬 6
[original](<https://github.com/stass/type-c>) · [HN discussion](<https://news.ycombinator.com/item?id=50602561>)

## Summary

Type-C 是一个为 C11 语言注入现代类型安全特性的编译器工具。它在保留 C 语言原有语法、指针和头文件机制的基础上，增加了变体类型、选项类型、泛型以及穷尽模式匹配等 C 语言原本缺失的功能。Type-C 的编译器 typec 将代码转换为可读的 C 代码，再交由 GCC 或 Clang 编译，无需运行时支持。开发者可以逐个文件迁移，逐步提升代码安全性，同时享受 C 语言的高效与灵活。

## Key quote

> Type-C 让 C 语言拥有了它一直缺失的类型安全，同时保持了 C 的简洁与高效，无需运行时，无需重构整个项目。

## HN discussion (6 comments)

### [adsharma](<https://news.ycombinator.com/item?id=50621482>)

> 为什么不使用 vlang？
>
> 我也曾想在 Python 上做同样的事情，于是写了一个 Python 变体的解析器。
>
> 它在 pypi 上叫 spy-ast

### [onlyrealcuzzo](<https://news.ycombinator.com/item?id=50621118>)

> \> 当然还有 Swift、Ocaml 或 Rust
>
> 想必 C++ 才是你首先应该提到的。

### [stass](<https://news.ycombinator.com/item?id=50603065>)

> 意识到这篇帖子在数据方面略显单薄，所以这里补充一些。
>
> 我为了测试，用 Type-C 重写了 tomlc99 的解析器（2k 行代码）。我在错误处理路径中使用了 result/try/defer，其余部分保持不变，并链接到相同的 C 程序和测试用例。
>
> 在约 1500 次运行中，每次运行注入一个分配失败：原始 C 代码出错 34 次（25 次内存泄漏，9 次释放后使用）；而重写后的代码从未出错，sanitizers 检查结果干净。
>
> 使用原始的 toml-spec-tests 文件：原始代码在 1002 个测试中发生泄漏，在 9 个测试中崩溃；重写后的代码两者皆无。
>
> 以下是表达式求值器代码的示例：
>
> 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)));
>
> …
>
> 欢迎大家提问！

---

## Cite

- Page: https://zeli.app/zh/story/50602561
- Date: 2026-11-22
- Source: github.com (https://github.com/stass/type-c)
- Hacker News: https://news.ycombinator.com/item?id=50602561

## Other languages

- en: https://zeli.app/story/50602561 · markdown: https://zeli.app/story/50602561.md
- ja: https://zeli.app/ja/story/50602561 · markdown: https://zeli.app/ja/story/50602561.md
- ko: https://zeli.app/ko/story/50602561 · markdown: https://zeli.app/ko/story/50602561.md
- de: https://zeli.app/de/story/50602561 · markdown: https://zeli.app/de/story/50602561.md
- es: https://zeli.app/es/story/50602561 · markdown: https://zeli.app/es/story/50602561.md
- ru: https://zeli.app/ru/story/50602561 · markdown: https://zeli.app/ru/story/50602561.md
