On Aug 12, 2010, at 7:52 PM, Tim Daly wrote:
I find that I'm horribly confused at this point about what a protocol "is". Can someone use some other comp. sci. terms to define this idea? I thought of them as Java interfaces with default methods but clearly I'm wrong.
Coming from CL, the best analogy is that a protocol is a *named* set of generic functions that dispatch on the type of their first argument. When you define a protocol with foo/bar/baz methods you get generic functions in the protocol's namespace named foo/bar/baz.
Unlike interfaces, protocols require no derivation. You can extend a protocol to a particular type by supplying a concrete map of method names to function objects (extend), *or* the code for same (extend- type), *or* by supplying definitions for the protocol methods inline in a deftype/defrecord. The latter *looks* a lot like implementing an interface, by design it is as easy to use, and might actually involve implementing an interface under the hood (but that is an implementation detail, enabled by the naming and first-arg dispatch restriction).
Finally, from Java-land, you can get your type to extend a protocol by deriving from the protocol's corresponding interface. This is what seems to confuse people most, as if protocols are magic dynamic interfaces. They are not. They *sometimes* use interfaces in the implementation, but not always. At no point should one assume that because a type supports a protocol it 'isA' that protocol (or its interface).
Protocols are not about inheritance and that is the key distinction vs interfaces.
Hope that helps, Rich -- 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