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,<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...
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.<p>I assume pattern matching must be exhaustive. Can there be a default case?
What is this sorcery?<p><pre><code> #include <datatype99.h>
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't dare to open the file datatype99.h lest it casts a dark spell on me.
> 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'd expect.<p>You can build Boost out of C99 macros. There's a really good reason why you shouldn't.