Live data from Hacker News

Grim C++ Tales from the Crypt: The Visitor Pattern (2017)

cppcrypt.tumblr.com

41–50 of 64 posts

Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)

#41
post #32

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.

Why have functions and control structures when we can just jmp?

Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)

#43
post #17
post #10

C++ 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.

For those working with C++ an answer might come in C++23.

http://open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1371r0....

Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)

#45
post #32

Earlier 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.

Again, Eli Bendersky has an excellent article about the issue. In particular, he notes that this has been discussed for the case of C++: https://eli.thegreenplace.net/2016/a-polyglots-guide-to-mult...

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)

#46
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.

Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)

#47
post #46

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.

Welcome to Tumblr.

Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)

#48

The only time I have ever encountered a code base where the visitor pattern was necessary and fulfilling its purpose is Babel (the JS compiler).

Agreed. The _only_ times I've ever been justified in implementing this pattern have been when doing compiler work.

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)

#49

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…

Yes, this is the only place I have ever used the visitor / double dispatch pattern. The main idea is it fulfills the "Open-Closed" principle:

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.

Post reply on HN