Earlier quoted context omitted.
It's a shortcoming of languages that don't have multiple dispatch.
Why should anyone force changes into the core language if all you want to do is use an obscure programming construct in a corner case that's easily implemented with a design pattern? Some of these complains are at best very misguided.
Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
41–50 of 64 posts
Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#42I'd probably just do type checks and maybe use a 2D table for the possible interactions.
Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#43C++ match expressions would make std::variant a really nice alternative. I assume there is already a proposal out?
std::visit mostly allows you to do that already: https://coliru.stacked-crooked.com/a/be5c44281eea8bc4 Then only unfortunate missing piece of the puzzle is that there's no trivial way to create a closure out of this, so it requires a bit more manual work to propagate local state to the visitor.
http://open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1371r0....
Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#44I clicked the link, hoping to disable x,y,z and there is just text. No options!
Not reading further.
Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#45Earlier quoted context omitted.
It's not a "shortcoming" of the language per se . Visitor is a mitigation of the Expression Problem[0] on the Object Oriented spectrum. Object oriented designs need to use Visitor to add functionality to existing data structures, whereas doing such is a natural feature of Functional languages (though FP languages have their own awkward angle in attempting to add new data structures to existing functionality). This is…
It's a shortcoming of languages that don't have multiple dispatch.
A follow-up goes into Python: https://eli.thegreenplace.net/2016/a-polyglots-guide-to-mult...
Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#46Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#47What 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.
Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#48The only time I have ever encountered a code base where the visitor pattern was necessary and fulfilling its purpose is Babel (the JS compiler).
Someone else up-thread mentioned what I think is the key requirement making visitors worthwhile is non-trivial tree traversal. ASTs seem to fit this description more than any other data structure I've had to work with day-to-day.
Outside of language trees, I wonder where else visitors are common?
Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#49The 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…
https://en.wikipedia.org/wiki/Open–closed_principle
Otherwise you find that your AST data structure is never finished -- you are constantly added stuff to it. I wish there was a clearer way in C++, but this is the C++ solution.