Hi,

On Jul 12, 10:44 am, Nicolas Oury <[email protected]> wrote:

> After playing a bit with protocols I found this (which seems odd to me):
>
> (All of this on clojure-1.2.0-master-SNAPSHOT, downloaded with lein - i
> don't know how recent it is)
>
> user> (defprotocol Foo (foo [x] ))   ; We define a protocol Foo
> user> (def bar {:foo Foo})   ; We hide it in a record
> #'user/bar
> user> (= (:foo bar) Foo)  ; It's still equal to itselffrom
> true
> user> (extend Object (:foo bar) {:foo (fn [x] :bar)})   ; We extend it
> nil
> user> (foo 5)   ; It works
> :bar
> user> (= (:foo bar) Foo)  ; it's not equal to itself anymore
> false
>
> By the way, if you look at the extenders of each:
> user> (extenders (:foo bar))
> nil
> user> (extenders Foo)
> (java.lang.Object)
>
> Foo is extended but not its copy.
>
> Is it what it is supposed to do?

I think this is the expected way. Since clojure emphasises
immutability you don't modify a Protocol by extending it to some type,
instead you get back a new version of it extending to the type.
Multimethods work similarly, IIRC.

Your previously stored version of the protocol is the old one, which
is not extended. Hence the nil on the extenders call. (foo 5) however
sees the new protocol and hence does the right thing.

This is backed solely by a gut feeling of mine, but I would not be
surprised if it were close to the truth.

Sincerely
Meikel

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to