3 ms·
How is multiple dispatch different from function overloading, as found in C++/Pascal? Is it the same thing?
by CyberShadow 12y ago
How is multiple dispatch different from function overloading, as found in C++/Pascal? Is it the same thing?
- brandonbloom 12y agoDispatch is the process of selecting and executing the matching overload. Multiple-dispatch refers to the ability to select the best overload based on more than just the first argument. C++ can do arity and type-based multiple-dispatch, but only statically. Julia supports dynamic multiple dispatch, where as C++'s virtual methods are only single-dispatch dynamically.
- StefanKarpinski 12y agoPrecisely. This may sound pedantic or academic, but in practice when static overloading and dynamic dispatch differ, the static behavior is usually not what you want. In my experience, this makes it painful to make code that is polymorphic in multiple arguments behave correctly in C++ (double dispatch [1] is one of the necessary but awkward tactics that's used), whereas multiple dispatch just works. And of course, almost working and actually working are a world apart even though they sound so close. [1] http://lostechies.com/derekgreer/2010/04/19/double-dispatch-is-a-code-smell/ http://lostechies.com/derekgreer/2010/04/19/double-dispatch-...
- epidemian 12y agoFrom playing a bit with the Julia REPL (http://forio.com/julia/repl/ http://forio.com/julia/repl/), it seems that the dispatch of the method is dynamic, as opposed to the dispatch of overloaded functions in C++: julia> function foo(s::String) "$(s)!!!" end foo (generic function with 1 method) julia> function foo(n::Int) n + 1 end foo (generic function with 2 methods) julia> foo(rand() < 0.5 ? "Hello" : 41) 42 I'm not used to this kind of dispatch for bare functions, but it seems to me that it could offer similar capabilities to what subtype polymorphism provides in OO†, without having to define new types for this methods to live in or polluting the existing types. † Probably more, as the dispatch can be resolved based on many arguments.
- deleted 12y ago[deleted]
- ihnorton 12y agoStefan Karpinski wrote a very nice overview of multiple dispatch with many examples and some discussion of design implications: http://nbviewer.ipython.org/gist/StefanKarpinski/b8fe9dbb36c1427b9f22 http://nbviewer.ipython.org/gist/StefanKarpinski/b8fe9dbb36c... (he gave this as a talk at Strange Loop 2013, but the video seems to still be embargoed)
- StefanKarpinski 12y agoThis may be my fault for not submitting the slides. I haven't figured out how to turn the IJulia notebook into a PDF :-\
- ViralBShah 12y agoI am assuming you have already tried this - but doesn't print and save as pdf do it on a mac?
- peatmoss 12y agoI think Chrome for other platforms will print to PDF conveniently too.