TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Datatype99: C99 with Sum Types, v0.1.0

157 pointsby hirrolotover 4 years ago

11 comments

syockitover 4 years ago
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&#x27;t just memcpy an existing tree to a malloc&#x27;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,<p><pre><code> void copy(BinaryTree *dest, const BinaryTree *src) { match(*src) { of(Leaf, x) { *dest = TREE(Leaf(x)); } } } </code></pre> 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...
评论 #26025137 未加载
评论 #26025142 未加载
naaskingover 4 years ago
I created something similar [1] awhile back:<p><pre><code> SUM(foo) { foo_one, foo_two, }; &#x2F;* declare each sum case *&#x2F; 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(&quot;foo_one: %d, %c\n&quot;, y-&gt;i, y-&gt;c); AS(foo_two, y) printf(&quot;foo_two: %d\n&quot;, y-&gt;d); MATCHANY fprintf(stderr, &quot;No such case!&quot;); exit(1); } } </code></pre> [1] <a href="https:&#x2F;&#x2F;github.com&#x2F;naasking&#x2F;libsum" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;naasking&#x2F;libsum</a>
kzrdudeover 4 years ago
It&#x27;s syntax sugar for tagged unions, of course. It&#x27;s not entirely out there that C could include such support natively in a future standard.
评论 #26024883 未加载
steerablesafeover 4 years ago
If `match` compiles down to a switch case on the type discriminator then it can easily beat current implentations of C++&#x27;s std::visit on std::variant.<p>I assume pattern matching must be exhaustive. Can there be a default case?
评论 #26025315 未加载
评论 #26025780 未加载
ciesover 4 years ago
&gt; Pattern matching is exhaustive too.<p>Sweet. C seems to be getting there :)
评论 #26025745 未加载
dleslieover 4 years ago
Oh, this is from the poica author! Neat!<p>That Poica can&#x27;t work on C99 made it a non-starter for me; but all I really wanted was the sum types.
评论 #26027698 未加载
geonover 4 years ago
How does it compare to the tagged unions in Zig, which aims to be “C with the bugs fixed”?
评论 #26025126 未加载
评论 #26025129 未加载
marcodiegoover 4 years ago
Can it be used to implement something similar to typen_ame() or type_id()?
评论 #26026055 未加载
enriqutoover 4 years ago
What is this sorcery?<p><pre><code> #include &lt;datatype99.h&gt; datatype( BinaryTree, (Leaf, int), (Node, struct BinaryTree *, int, struct BinaryTree *) ); </code></pre> What kind of preprocessor black magic can you do so that this code compiles? I don&#x27;t dare to open the file datatype99.h lest it casts a dark spell on me.
评论 #26024839 未加载
评论 #26024812 未加载
评论 #26024757 未加载
fallatover 4 years ago
Combine it with Checked C...Why do we need Rust? :)
评论 #26024925 未加载
评论 #26025440 未加载
setpatchaddressover 4 years ago
&gt; Pure C99. No external tools are required; datatype99 is implemented using only preprocessor macros.<p>And sure enough, the code is unreadable and unmaintainable as you&#x27;d expect.<p>You can build Boost out of C99 macros. There&#x27;s a really good reason why you shouldn&#x27;t.
评论 #26032245 未加载