Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

anyone care to comment on whether multiple dispatch or multimethods (e.g., in CLOS) solves this problem more elegantly?


view as:

Indeed. Good old Steve misses the point again...

yes. that is quite simply even with ruby, the wrong way to tackle that problem.

The classic OOP solution to that class of the problem is the visitor pattern, so yes, multiple dispatch to the rescue.

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


Uh, no. The pattern has trade offs, that doesn't make it useless, it most certainly solves problems.

I only skimmed the article but it looks like he is more or less talking about the expression problem brought up by Philip Wadler some time ago. Definitely multiple dispatch can solve this problem but if we stick to the original definition of the problem it breaks on the requirement for static type safety (which probably doesn't matter to you heh).

GADTs in haskell and I think Scala can solve this cleanly. Active Patterns in F# may also provide a clean solution for the full problem.


Yes, you can solve it quite simply with an active pattern, or type union. The fundamental problem with the problem is that it's a relational problem and polymorphism largely solves hierarchical problems. This type of problem also solves easily in prolog. The solution to the problem in java is to create some kind of relation class that abstracts the state of the relationship to between individuals/groups.

Legal | privacy