The thing that annoys me most about this pattern is that people always miss the "there needs to be lots of things you want to perform on those thingies".<p>And so we end up with a huge pile of visitor code, and there is one or maybe two actual visitors.<p>When it would have been far easier to read and modify if it was just coded out normally.
If you're like me and couldn't get past the first few slides because it's a terrible reading experience, go to the archive, which shows the whole thing in thumbnails. It makes it little better, even though the thumbnails are in reverse:<p><a href="https://cppcrypt.tumblr.com/archive" rel="nofollow">https://cppcrypt.tumblr.com/archive</a>
The visitor pattern isn't appropriate everywhere, but it's quite good for traversing and manipulating ASTs (abstract syntax trees). An AST typically has many kinds of nodes, but programs that traverse the AST usually only care about some small subset of those nodes. The visitor pattern provides a clean solution: a generic AST traversal library visits all nodes in the tree while allowing the program to customize what happens when visiting specific nodes. I've found the visitor pattern very effective for creating derivative ASTs without knowing about everything that might be in the AST.
It's things like the visitor pattern that led me to implement Clasp - a Common Lisp that interoperates with C++ and uses llvm as the backend (github.com/clasp-developers/clasp.git).
Common Lisp has generic functions and multiple dispatch - so it doesn't need the wretched visitor pattern.
Try writing a compiler using the visitor pattern - I dare you. :-)
This... I can actually see the point in this. The Visitor pattern is one of those patterns I never really saw the point of, and which mostly struck me as an over-engineered hack about a shortcoming in a language.<p>This example actually makes clear why you'd need to do it this was in C++ at least. Not sure which other languages would need this. It's certainly not pretty. Then again, dispatching twice is not so bad compared to your average Java-style over-engineering.
I clicked the link and then tumblr asked me about privacy, with a link to "Manage options".<p>I clicked the link, hoping to disable x,y,z and there is just text. No options!<p>Not reading further.
What a terrible UI on that website. Constantly clicking "Next"! Yuk. And every page required zooming in and out to make it fit on my screen. For a tech focused comic, you'd think the author could run it through an ImageMagick batch resize job. Bonus points for allowing keyboard back and forth navigation.
While I, as a rule, try not to stand against good storytelling, I clicked far too many times while thinking "get to the point" before I just closed the tab.
The example code the author comes up with:
<a href="https://github.com/dpugson/examples/blob/master/chpt1_the_visitor_pattern/example.cpp" rel="nofollow">https://github.com/dpugson/examples/blob/master/chpt1_the_vi...</a><p>Which implements the pseudocode:
<a href="https://cppcrypt.tumblr.com/post/168134402897" rel="nofollow">https://cppcrypt.tumblr.com/post/168134402897</a><p><pre><code> main {
thingies = [ purpleThingy, littleThingy ]
interactions = [ commentOn, cherish ]
for (interaction in interactions)
for (thing in thingies)
interaction.interact(thing)
}
</code></pre>
The author does mention things like "function pointers can be used in simple cases" and "std::variant would avoid the need to overload the method". But the main reason for having the C++ code the way it is is because "you can't dispatch to overloaded methods at runtime".
<a href="https://cppcrypt.tumblr.com/post/169439207562" rel="nofollow">https://cppcrypt.tumblr.com/post/169439207562</a> ff.
The thing with the visitor pattern is that it indeed is useful for many operations over fixed data. But the same holds for simple pattern matching. Inheritance (or rather subtyping) is useful for changing data but a fixed set of operations. But what the eff do we do when we have many operations on changing data? The expression problem still is an interesting problem, because a data structure that solves it would basically be the "gold standard".
I don't understand why the class instance needs to accept the interaction. Why not just invoke the interaction directly?<p><pre><code> interactor_p->interact(thingy_p)
</code></pre>
Would this still be considered the visitor pattern or is the extra layer of indirection important?
When I originally read about Visitor in the GoF book it was fun to work through its convoluted way but seemed ugly in any language, including the original SmallTalk.<p>I'd probably just do type checks and maybe use a 2D table for the possible interactions.
I enjoyed the format of this, kept me interested. Nowadays I feel like if I read a blog post I have such a short attention span I'd just glance over it.<p>Would love to see more stories.
If you ask me, the actual problem is the tree data structure... Maybe post order forms lend themselves better to processing.<p>The even deeper problem is what ASTs are meant to <i>represent</i>, to which the answer would be "possibly a lot of diverse things". Diversity is never good in a computational context. But I don't see a good way to avoid it in the context of programming languages and ASTs. The reason for the diversity is that programming languages should allow humans to specify what should happen in very few keystrokes.