编程与软件工程github.com
用 C99 实现代数数据类型
Algebraic Data Types for C99
2021 — 2024,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
datatype99 是一个面向 C99 的库,用宏在 C 里实现代数数据类型(本质是带标签的联合体)和模式匹配,让 C 代码也能像 ML/Haskell 那样定义 sum type 并分支处理。项目给出了二叉树等示例,展示如何用声明式语法替代手写 tag + union + switch。
为什么经久不衰:它用极轻量的宏把函数式语言的类型抽象带入 C,无需改编译器或运行时,对系统程序员长期有吸引力。
HN 评论精选
批评项目缺少堆分配与深拷贝的示例,实际使用时会遇到指针归属问题。
我经常发现 HN 上介绍的 C 扩展项目会忽略动态分配对象的示例。在二叉树例子里,显然不能直接把已有的树 memcpy 到 malloc 出来的树,因为节点会指向旧树上的节点。我写了一半递归拷贝函数,然后意识到它仍然指向栈上的叶节点,需要一种把所有指针改为相对于目标树的方式。
syockit查看英文原文
Too often that I find the C extension projects introduced here on HN would omit examples for dynamically allocated objects. In the binary tree example, apparently I can't just memcpy an existing tree to a malloc'd tree because the nodes would point to the ones on the old tree. I was halfway writing a copy function that recursively traverses the source tree, void copy(BinaryTree *dest, const BinaryTree *src) { match(*src) { of(Leaf, x) { *dest = TREE(Leaf(x)); } } } then realised that this still points to a leaf on the stack. Need a way to replace all the pointers to be relative to the destination tree...
指出已有同类实现 libsum,并展示其 SUM/CASE/MATCH 语法。
我以前做过类似的东西(libsum):用 SUM 声明和类型,用 CASE 声明各分支的字段,用 MATCH/AS 做模式匹配,还可以用 MATCHANY 处理未匹配情况。
naasking查看英文原文
I created something similar [1] awhile back: SUM(foo) { foo_one, foo_two, }; /* declare each sum case */ CASE(foo, foo_one) { int i; char c; }; CASE(foo, foo_two) { double d; }; void do_bar(foo f) { MATCH(f) { AS(foo_one, y) printf("foo_one: %d, %c\n", y->i, y->c); AS(foo_two, y) printf("foo_two: %d\n", y->d); MATCHANY fprintf(stderr, "No such case!"); exit(1); } } [1] https://github.com/naasking/libsum
认为它是 tagged union 的语法糖,但有望进入 C 标准。
这当然只是带标签联合体的语法糖。C 未来的标准并非完全不可能原生加入这种支持。
kzrdude查看英文原文
It's syntax sugar for tagged unions, of course. It's not entirely out there that C could include such support natively in a future standard.
看好其性能潜力,并追问穷尽匹配下是否支持 default。
如果 match 能编译成基于类型判别器的 switch,那它很容易胜过当前 C++ 对 std::variant 的 std::visit 实现。我猜模式匹配必须是穷尽的,那能不能有 default 分支?
steerablesafe查看英文原文
If `match` compiles down to a switch case on the type discriminator then it can easily beat current implentations of C++'s std::visit on std::variant. I assume pattern matching must be exhaustive. Can there be a default case?
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选