From what I understand, the visitor pattern is supposed to solve the expression problem (described here), where a program needs to support performing multiple operations on multiple types, ideally allowing adding new operations and new types without touching existing code.
- OOP languages can define a method for each operation on each type of object; this makes it easy to add new objects without modifying existing code, but adding a new operation requires modifying all existing objects.
- FP languages with pattern matching have the opposite issue; adding a new operation is self-contained, but adding a new data type requires modifying all existing functions to support the new type.
The visitor pattern, as I understand it, just changes the OOP style to the FP style; adding a new operation just means adding a new type of visitor, but adding a new data type means adding a method to all existing visitors. Is my understanding correct? If so, what's the benefit of the visitor pattern, if it doesn't fully solve the expression problem?