This chapter covers
- Mimicking objects with multimethods (Single dispatch)
- Multimethods where implementation depends on several argument types (Multiple dispatch)
- Multimethods where implementation depends dynamically on several arguments (Dynamic dispatch)
Object-Oriented Programming is well known for allowing different classes to be called with the same interface, via a mechanism called polymorphism. It may seem that the only way to have polymorphism in a program is with objects. In fact, as we are going to see in this chapter it is possible to have polymorphism without objects via multimethods.
Moreover, multimethods provide more advanced polymorphism than OOP polymorphism as they support cases where the chosen implementation depends on several argument types (multiple dispatch) and even on the dynamic value of the arguments (dynamic dispatch).
For today’s session, Dave has invited Theo to come and visit him at his parent’s house in the countryside. As Theo’s drive takes him from the freeway to increasingly rural country roads, he lets himself be carried away by the beauty of the landscape, the smell of fresh earth, and the sounds of animals in nature. This 'nature bath' puts him in an excellent mood …
Dave receives Theo in jeans and a T-shirt, a marked contrasts with the elegant clothes he wears at the office. A straw hat completes his country look.