I've been reading around trying to understand multiple dispatching, and why it's so special.
On Wikipedia I came across this simple example:
(defmethod collide-with ((x asteroid) (y asteroid))
;; deal with asteroid hitting asteroid
)
(defmethod collide-with ((x asteroid) (y spaceship))
;; deal with asteroid hitting spaceship
)
(defmethod collide-with ((x spaceship) (y asteroid))
;; deal with spaceship hitting asteroid
)
(defmethod collide-with ((x spaceship) (y spaceship))
;; deal with spaceship hitting spaceship
)
What I don't understand however, is how it's different from just doing something like this:
class Asteroid {
def collide(y: Asteroid) = // deal with asteroid hitting asteroid
def collide(y: SpaceShip) = // deal with asteroid hitting spaceship
}
class SpaceShip {
def collide(y: Asteroid) = // deal with spaceship hitting asteroid
def collide(y: SpaceShip) = // deal with spaceship hitting spaceship
}
On the technical level, what is the difference? I know one uses multiple dispatch, and the other uses single dispatch with method overloading.
As I understand it, the visitor pattern is a solution to get around not having multiple dispatch in single-dispatch OOP languages, which just confuses me more since the overloading solution seems to be working just fine without employing this pattern.
So can someone explain the technical differences between doing one vs the other?