Programming languages should have a tree traversal primitive
Published on: 2025-08-04 19:23:19
There should be a control flow construct in programming languages that can handle tree-like traversal in a nice way, similar to how for/foreach loops can handle linear traversal. It's a bit of a missing gap in the current set of control flow constructs most languages these days have settled on. Its a thing I end up having to do *all the time* and it seems like there should be some shortcuts for it.
I posted a thought about this recently and was thinking about how I would want something like that to look like. I can't find anything similar in any other languages. I'm most familiar with C++, so my sample code here is going to be C++-ish, but its general enough that you could fit it into basically anything else. I'm not a language design expert, so this is more of a loose idea than a formal proposal.
for_tree(Node* N = mytreeroot; N != NULL; N : {N->left, N->right}){ print(N->value); }
with the specifics removed, this is *almost* the same syntax as an existing for loop
for_tree(init;
... Read full article.