When you create a protocol, as an implementation detail, it also creates a
Java interface.

When you list protocols in a deftype or defrecord form, the generated class
actually implements that Java interface.  And protocol calls to that type
call through the interface. This gives the best performance.

If you add protocols to an existing class, record or type; this dispatch is
done dynamically and interfaces aren't involved.

-- 
Dave
On Mar 7, 2012 8:09 AM, "Tassilo Horn" <tass...@member.fsf.org> wrote:

> Garth Sheldon-Coulson <garth.sheldoncoul...@gmail.com> writes:
>
> Hi Garth,
>
> > I'm returning to Clojure in earnest for the first time since 1.1 (and
> > very happy to be back!). Apologies if this question revisits old
> > issues.
>
> I've revisited mostly the same 2 days ago: <
> 87399ocg9a....@thinkpad.tsdh.de>
>
> > I'm trying to understand why the semantics of protocols are such that
> > the third statement here returns true:
> >
> > user=> (defprotocol Bashable (bash [this]) (boom [this]))
> > Bashable
> >
> > user=> (defrecord Record [] Bashable (bash [this] "bash!"))
> > user.Record
> >
> > user=> (and (satisfies? Bashable (Record.)) (extends? Bashable
> > Record))
> > true
> >
> > This returns true even though boom is not implemented for Record:
> >
> > user=> (boom (Record.))
> > AbstractMethodError user.Record.boom()Ljava/lang/Object;  user/eval55
> > (NO_SOURCE_FILE:3)
> >
> > Apparently, types/records can implement a protocol "in name only."
>
> What do you mean with "in name only"?
>
> > What is behind this choice? Intuitively, I would have conceived of a
> > protocol as a collection of methods all of which must be implemented
> > in order for a type/record to extend the protocol.
>
> I think it would be nice if extends?/satisfies? would return the set of
> implemented methods, say, as keywords.  Then you could check if an
> object implements exactly the methods that your function wants to call.
>
> > A second question. How does one "explicitly" extend a protocol per the
> > documentation of the extenders function? Intuitively, I would have
> > thought that the code above "explicitly" extends Bashable if it
> > extends Bashable at all. Yet:
> >
> > user=> (extenders Bashable)
> > nil
>
> That was my initial question in my posting two days ago.  extenders
> returns only types that extend a protocol using the `extend' function
> (or `extend-type', `extend-protocol' macros).  So this works:
>
>  user> (defprotocol Bashable (bash [this]) (boom [this]))
>  Bashable
>  user> (defrecord Record [])
>  user.Record
>  user> (extend-protocol Bashable
>           Record (bash [_] "Bash!"))
>  nil
>  user> (extenders Bashable)
>  (user.Record)
>
> IMO, that behavior is not very obvious.
>
> OTOH, Stu said that extenders is specific to the extend form (as
> suggested by its name), and in fact, neither defrecord's nor deftype's
> documentation speak of extending a protocol, but their terminology is to
> supply method implementations for protocol methods.
>
> But then, what's the use case of `extenders' anyway?  I mean, I want to
> know what types participate in a protocol.  I don't care if they do
> because they were explicitly extended to the protocol or because the
> method implementations were provided directly in their definition form.
> That's an implementation detail I shouldn't have to bother with...
>
> Bye,
> Tassilo
>
> --
> 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 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