On Jan 4, 9:40 pm, Raoul Duke <rao...@gmail.com> wrote:
> > It is interfaces that are more restrictive than protocols, and extend
> > can't fix that. This ability to extend an existing type to a protocol
> > is a main reason protocols exist.
>
> i guess i'm horribly confused about the right mental model for all of
> this, apologies.
>
> e.g. it sounds like the only way i can get a type to participate in an
> interface is if i control the source file that has the deftype. i
> thought in previous versions of the new branch i could use extend to
> get a type i don't own to participate in an interface, which seems
> desirable. maybe i am horribly confabulating. perhaps the idea would
> be to make a new protocol that 'mirrors' the already existing
> interface somehow?
>

Broadly speaking yes; you can only have a type participate in an
interface if you control the type; because interfaces work via Java
methods; i.e. you invoke them with "." syntax, like: (.compareTo c1
c2).  Protocols, on the other hand, register their "methods" as
clojure functions, which you call as a normal function, e.g. (compare-
to c1 c2).  In order to make a type support an interface, you'd
literally have to change the class to add definitions for the
interface methods, which you can't (for the purposes of this
dicussion) do once the class has been loaded.  Since protocol
"methods" are just Clojure "functions", there is under-the-hood
machinery that allows them to support implementation of a protocol on
a type via a number of different mechanisms; protocol methods
implemented inside a deftype construct are translated into traditional
java-style methods, while protocol methods implemented with "extend"
generate normal clojure functions which are then "hooked" into the
protocol system (this is the reason that protocols implemented inside
deftype are "faster" than those implemented with "extend").

This is what Rich means when he says that "It is interfaces that are
more restrictive than protocols".  The problem of "adding" an
interface to a type is often referred to as the "expression problem",
particularly in the Clojure documentation around types / protocols.

Hope this helps (apologies if anything's glaringly wrong...),

-DTH

-- 
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

Reply via email to