Not mentioned in Cedric's post are two other important things: Protocols can be extended to existing types. For example:
(defprotocol IType (type-as-string [x])) (extend-protocol IType String (type-as-string [x] "string") Integer (type-as-string [x] "integer")) => (type-as-string 42) "integer" Here we are adding new methods to "sealed" closed classes that already exist in the JVM. We never modify these classes, we simply extend our protocol to them. Secondly, all protocol functions are namespaced. This allows us to extend classes without fear of overwriting existing methods. This then is more powerful than monkey patching in Ruby or Python as the resulting method is more like 42.user_type-as-string(). Clojure's namespace system then allows you to refer to one method or the other just as you would any normal functions. The net result is that protocols completely solves the expression problem, and in a way that doesn't require tons of boilerplate code (like the visitor pattern, for example). Timothy On Sun, Dec 29, 2013 at 12:38 PM, Massimiliano Tomassoli <kiuhn...@gmail.com > wrote: > On Sunday, December 29, 2013 7:05:28 PM UTC+1, Cedric Greevey wrote: > >> On Sun, Dec 29, 2013 at 12:27 PM, Massimiliano Tomassoli < >> kiuh...@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. >> > > OK. I was reading about the "Expression Problem" and how it can supposedly > be easily solved in Clojure. That should mean that Clojure is a very > powerful language. But I can do the same thing in C++. If I use simple > structures or classes without methods, I can solve the "Expression Problem" > the same way as Clojure does. Moreover, I can build dynamic dispatching > into C++ just by including a property "type" in each structure and by using > templates and maybe a few macros to add some syntactic sugar. Doesn't this > prove that C++ is at least as powerful as Clojure w.r.t. the "Expression > Problem"? > >> -- > -- > 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. > -- “One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.” (Robert Firth) -- -- 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.