5 ms·
A polyglot's guide to multiple dispatch – part 4
- sratner 11y agoReally enjoyed this series. Previous parts: https://news.ycombinator.com/item?id=11526923 https://news.ycombinator.com/item?id=11526923 [Intro, C++] https://news.ycombinator.com/item?id=11541526 https://news.ycombinator.com/item?id=11541526 [Python] https://news.ycombinator.com/item?id=11571451 https://news.ycombinator.com/item?id=11571451 [CLOS]
- aerovistae 11y agoDidn't notice the part 4 at first. Initial thought was, "My god, how has this article been on the front page for two straight weeks?? Must be an incredible write-up."
- malisper 11y agoI posted this on the blog but I'm copying it here because I have to wait for approval on the blog. If you didn't know, Clojure's dispatch mechanism is based on the Common Lisp library, filtered functions[0], which is described in this paper[1]. [0] https://github.com/pcostanza/filtered-functions https://github.com/pcostanza/filtered-functions [1] http://www.p-cos.net/documents/filtered-dispatch.pdf http://www.p-cos.net/documents/filtered-dispatch.pdf
- catnaroek 11y ago> In fact, multiple dispatch is best categorized as a generalization of OOP, being more in the domain of generic programming. Kinda like the generic programming of C++, just at runtime instead of being limited to compile-time constructs and metaprogramming. Doing it at compile-time is a feature, because the compiler has more information available for selecting the right specialization. (Technically, this can be done at runtime as well, but only at the price of making dispatch insanely expensive.) For example, in CLOS, how would I do the equivalent of specializing a template for when its argument is a `vector<list<unique_ptr<int>>>`?
- GregBuchholz 11y agoCould you clarify if you are trying to make a comparison between garbage collection and unique_ptr? Or are you saying that you wish CLOS allowed dispatch on types as well as classes (anyone know the historical reason for why this is the case?). Or are you making the point that it would be ugly to create a new class "vector-of-list-of-fixnums"? Or something else?
- catnaroek 11y agoIt has nothing to do with garbage collection. I could've used GHC Haskell with sufficient extensions (MultiParamTypeClasses, FlexibleInstances, perhaps UndecidableInstances too) enabled as an example, instead of C++. The point is that C++ has a much richer type structure on which you can dispatch.
- jorams 11y agoCLOS only allows dispatch on classes and not types, because there is no hierarchy in types. For example, if you have a method specializing on the type (satisfies a) and a method specializing on the type (satisfies b), and you call a generic function with an object that satisfies both a and b, which method should be called first? Or methods specializing on the types (integer 0 8) and (integer 1 9). If the argument is 5, which type is more specific? Classes are a subset of types with a certain hierarchy, so there is always a "correct" order for methods to be called in.
- pjmlp 11y agoJust an idea, most likely with macros.
- catnaroek 11y agoWhich is totally orthogonal to CLOS, though.
- gecko 11y agoThrough things like this library, which, combined with a Sufficiently Smart Compiler (SBCL qualifies as one for this purpose), produces C++-like static dispatch for cases such as the one you're proposing. https://github.com/guicho271828/inlined-generic-function https://github.com/guicho271828/inlined-generic-function
- deleted 11y ago[deleted]