News for hackers. Stuff that matters.
Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017) by mcv on Wednesday March 06, 2019 @10:29AM (#19319565)
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.

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.

Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017) by moron4hire on Wednesday March 06, 2019 @11:42AM (#19320247)
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 a big motivator for most modern programming languages supporting multiple design paradigms.

[0] https://eli.thegreenplace.net/2016/the-expression-problem-an...

Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017) by barrkel on Wednesday March 06, 2019 @12:04PM (#19320515)
It's a shortcoming of languages that don't have multiple dispatch.
Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017) by moron4hire on Wednesday March 06, 2019 @01:21PM (#19321186)
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) by geezerjay on Wednesday March 06, 2019 @12:55PM (#19320966)
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.

Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017) by int_19h on Wednesday March 06, 2019 @04:49PM (#19323212)
Have you considered that multiple dispatch is obscure because it requires jumping through such hoops to implement in most mainstream languages, rather than because of its lack of utility?
Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017) by kazinator on Wednesday March 06, 2019 @03:20PM (#19322373)
The short answer is that the existence of multiple dispatch isn't motivated by the visitor pattern.

Moreover, multiple dispatch isn't obscure at all.

C++ has it, but only statically, for instance, in the form of overloaded functions. C++ chooses a function overload by looking at the types of all parameters and arguments.

Multiple dispatch is similar, but the run-time types of the arguments are used.

This is broadly applicable in making all sorts of situations nicer.

Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017) by bstamour on Wednesday March 06, 2019 @01:11PM (#19321094)
Why have functions and control structures when we can just jmp?
Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017) by geezerjay on Wednesday March 06, 2019 @04:27PM (#19322984)
That's a disingenuous comparison. Functions are used pervasively, but even the visitor pattern isn't used very often.
Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017) by bstamour on Thursday March 07, 2019 @09:11AM (#19328094)
But in languages that support multiple dispatch at the language level, they're just another way of writing polymorphic functions. They're not just a niche feature. When you don't have to write all of the ceremony of the visitor pattern, it's easier to use visitors everywhere without thinking about them.
Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017) by yvdriess on Wednesday March 06, 2019 @11:00AM (#19319872)
I've always heard that pattern itself is older than C++, originating from Smalltalk.
Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017) by scroot on Wednesday March 06, 2019 @12:21PM (#19320678)
It does come from Smalltalk, which is in part why it looks so sloppy and strange in other languages (a Ruby example I saw even had an intermediary class for the visitor part, which doesn't really happen in Smalltalk).

The reason to use this pattern is to leverage real polymorphism. In Squeak/Pharo, the graphics system is called Morphic, which uses this pattern all the time to draw different kinds of objects to different kinds of Canvases. With a visitor pattern, each object can "know" how to draw itself to a given canvas.

Re:Grim C++ Tales from the Crypt: The Visitor Pattern (2017) by cjhanks on Wednesday March 06, 2019 @08:07PM (#19324713)
A state machine with context is one case I have used it for.