3 ms·
I disagree with the main thrust of the article, that the Visitor pattern is primarily a primitive way to do pattern matching. For one, the Visitor pattern (as
by smallnamespace 2y ago
I disagree with the main thrust of the article, that the Visitor pattern is primarily a primitive way to do pattern matching.
For one, the Visitor pattern (as written in the article) fails to fulfill a core feature of Rust pattern matching, which is having a compact language to describe when something matches based on values, as opposed to the concrete type.
For another, you could equally well say that if-else blocks or ternary statements are also primitive pattern matching, if you're willing to stretch things that far.
In my view, the core reason people reached for the Visitor pattern in the past is that old Java didn't have convenient lambdas (anonymous functions). Visitors let you create an interface that mimics functional map.
Newer versions of Java have made lambdas much more convenient, so there's less motivation to reach for the Visitor pattern. You also saw this development in C#, where delegates were a language feature that often obviated rolling your own Visitors.
- acchow 2y agoHow do you achieve multiple dynamic dispatch with lambdas? Without pattern matching, don't you need the visitor to incrementally dispatch?
- quantified 2y agoVisitors let you do both. That said, I have used visitor a lot and it never was in a context where lambdas would have helped. There wasn't any variability in the code path such that I might have different implementations of a function at different times, and as the Visitor would have some state in it (an accumulator, a set of config variables, etc.) composing those as lambdas and dispatching to them seems like it would have been less convenient.