Hacker News new | past | comments | ask | show | jobs | submit login

I've learned about multi-dispatch from Common Lisp, and back then was prototyping a turn-based game, where I can have multi-method working:

    Outcome attack( RangeBaseUnit attackingUnit, MeleeBasedUnit defendingUnit ) 
and also

    Outcome attack( MeleeBasedUnit attackingUnit, RangeBasedUnit defendingUnit ) 
it was the most elegant approaches I've seen. Not that you can't do it in C++, but not as straightforward as it was in CLOS.



In OOP, isn't that essentially just overloading? In many OO languages, you could write both of those lines as-is (with corresponding method bodies) and have valid code. They'll typically be treated as two separate methods that just happen to have the same name.


The main difference is that overloading is resolved at compilation time whereas multi methods are resolved at runtime.


No because multi-mthods don't have a self/this pointer as such, rather they are dispatched at runtime taking into account the actual types of each parameter.


In OOP, I think the closest equivalent would be the visitor pattern.




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: