4 ms·
What do you mean by "multiple dispatch"? What's an example of a multiple dispatch program?
by PoignardAzur 2y ago
What do you mean by "multiple dispatch"? What's an example of a multiple dispatch program?
- Jtsummers 2y agohttps://en.wikipedia.org/wiki/Multiple_dispatch https://en.wikipedia.org/wiki/Multiple_dispatch It means dynamic dispatching based on the types of multiple arguments not a single argument.
- ebcode 2y agohere's an article with examples in Python and Julia: https://medium.com/@AlexanderObregon/how-pythons-multiple-dispatch-works-8c69e24c45a6 https://medium.com/@AlexanderObregon/how-pythons-multiple-di...
- kazinator 2y agoMultiple dispatch means that a method is selected based on the run-time type of more than one argument, rather than just "the object" (leftmost argument). This is beneficial to the Visitor Pattern, because the pattern needs to traverse a structure with polymorphic nodes, and invoke logic that is based on the type of each node, and on the type of the visiting object. The familiar single-dispatch Visitor Pattern introduces an emulation of double dispatch via two single dispatches. First a "accept" method is invoked on each Node of the traversed structure, taking the visitor as the argument. This accept method is a stub which calls the "visit" method on the visitor, passing the node as an argument. The static type of the node is known at that point, so the visitor can statically dispatch different overloads for different nodes. I possibly have some of this backwards, but it doesn't matter; it will work with different naming. Under multiple dispatch, we can just call a single generic function visit which is dispatched for the node and visitor in one step. If it is a printing visitor, it prints the node, and so on.