Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Multimethods and the condition system are 2 of my favorite things.


Genuine question here: when are multimethods useful in practice?


One of the less controversial uses of OO dispatch is to replace switch statements or chained ifs with a polymorphic function. Try to do this more than a few times to the same codebase, and you'll either wind up having to merge classes into weird hierarchies or use hacks like "the visitor pattern" to proceed.

The other big use of multimethods is when you have static-type function overloading (as in Java/C++) and decide you need to move the overload selection into runtime via the dynamic type of the arguments. This isn't possible in the general case without doing all the dispatch work yourself, but it's a trivial with multimethods.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: