On Sun, Dec 29, 2013 at 12:27 PM, Massimiliano Tomassoli <kiuhn...@gmail.com
> wrote:

> What's the difference between protocols and simple overloading?
>

Dynamic dispatch. Overloading uses just the static type for dispatch, so
this Java code:

aBase = new Base();
aDerived = new Derived();
aBase2 = aDerived;
something.foo(aBase);
something.foo(aBase2);
something.foo(aDerived);

will call the same version of foo the first two times, though the third
time it might call a different overload of foo (if there's a separate
foo(Derived x) method in the class of "something").

Dynamic dispatch uses the runtime type. In Java you get this when you call

aBase.bar();
aBase2.bar();
aDerived.ber();

and (if Derived overrides bar) get the Derived version of bar called for
the second as well as the third calls, because aBase2's runtime type is
Derived.

Clojure protocols dispatch on the runtime type of the first argument, much
like (non-static) Java methods (with "this" considered to be the first
argument).

Clojure multimethods can dispatch on the runtime type (or even other
characteristics) of *all* of the arguments.

I don't think anything Clojure does dispatches on the static type of
anything, only on the number of arguments and sometimes on runtime types or
other information, with the exception of some built-in inline functions
(arithmetic +, for example) on primitive numeric arguments, and possibly
also macros that encounter primitives.

Indeed, primitive locals are just about the only static types to be found
in Clojure (notwithstanding core.typed, whose use AFAIK does not influence
the dispatch semantics of anything).

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to