Hacker News new | ask | show | jobs
by barrkel 2662 days ago
It's a shortcoming of languages that don't have multiple dispatch.
2 comments

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

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.

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

Why have functions and control structures when we can just jmp?
That's a disingenuous comparison. Functions are used pervasively, but even the visitor pattern isn't used very often.
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.