
A polyglot's guide to multiple dispatch – part 4 - ingve
http://eli.thegreenplace.net/2016/a-polyglots-guide-to-multiple-dispatch-part-4/
======
sratner
Really 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]

------
malisper
_I 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
> 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
Could 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?

~~~
jorams
CLOS 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.

------
aerovistae
Didn'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."

