Matthew Phillips <mattp...@gmail.com> writes: > ;; A node has a data attachment and (possibly) children > (defprotocol Node > (data [n]) > (children [n])) > > (deftype SimpleNode [d] > Node > (data [n] d) > (children [n] [])) > > ;; In version 2, I add want to add pretty-printing > > (defprotocol PrettyPrintableNode > (pretty-print [n])) > > ;; Make anything potentially pretty-print'able > (extend-type Object > PrettyPrintableNode > (pretty-print [n] (str "A node: " (data n))))
I'm confused by the mixture of formality and looseness here. When you extend this way, binding this `pretty-print' implementation to arguments of type Object, you're implicitly requiring that argument "n" satisfies the protocol Node -- because you call the `data' function on it. But if the function `data' would work on any kind of Object, then so too would any call to `pretty-print', right? Should the call to `data' within the `pretty-print' definition above require some mention of protocol Node? In other words, what's the difference between using `extend-type' here on Object and just writing ,---- | (defn pretty-print | [n] | (str "A node: " (data n))) `---- My experiments in the REPL don't show any difference in behavior. Being able to define a normal `pretty-print' function above differs from the generic function system in Common Lisp. There, if one has defined a generic function with `defgeneric', and one later tries to define a normal function with the same name, the normal function replaces the generic function, and the compiler may emit a warning. Working the other way, though, one may not define a generic function -- using `defgeneric' or `defmethod' -- if the function name is already bound to a normal function, macro, or special operator. Is there supposed to be a difference between the normal function `pretty-print' I wrote above and the function defined in the `extend-type' form quoted above? -- Steven E. Harris -- 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