3 ms·
The classic OOP solution to that class of the problem is the visitor pattern, so yes, multiple dispatch to the rescue.
by gnaritas 16y ago
The classic OOP solution to that class of the problem is the visitor pattern, so yes, multiple dispatch to the rescue.
- skybrian 16y agoActually, the visitor pattern doesn't actually solve anything - it just moves the problem around. At best, it makes your code more complicated in return for getting a compiler error when you forget to implement something. Example: if you have 150 monsters then you would need a MonsterVisitor with 150 visit() methods. Now when you add a new monster, you have to add another visit() method and perhaps implement it in each visitor (unless you implement a default instead). And this doesn't work at all if you load new monsters at runtime, unless they always get the default behavior. There's little advantage over switch statements with defaults so long as you have a way of remembering where all the switch statements are when you need to update something.
- gnaritas 16y agoUh, no. The pattern has trade offs, that doesn't make it useless, it most certainly solves problems.