On a related note...
If I have something like this:
(defn make-archive [] (Archive. ))
(defrecord Archive []
java.io.Closeable
(close [_] (print "first try")))
user> (.close (make-archive))
first try
(defrecord Archive []
java.io.Closeable
(close [_] (print "second try")))
user> (.close (make-archive))
first try
user> (.close (Archive.))
second try
Apparently "make-archive" keeps a reference to the old class.
Should I just learn to evalute all my constructor functions when the
implementation changes?
2011/7/30 Andreas Liljeqvist <[email protected]>
> Thanks, that was very informative.
>
> I got no technical reason to expect that implementing an interface would
> define a function, lets just say that it felt right :/
>
> 2011/7/29 Aaron Cohen <[email protected]>
>
>> On Fri, Jul 29, 2011 at 3:18 PM, Andreas Liljeqvist <[email protected]>wrote:
>>
>>> Is this a bug?
>>>
>>>
>> Nope, expectation dissonance.
>>
>>
>>> (defprotocol Notcloseable (dosomething [this]))
>>>
>>>
>> This is what actually defines the function "dosomething". Once this
>> definition happens, the function exists and can be called. If you try to
>> call it before there are any implementations though you'll get:
>>
>> user=> (dosomething 1)
>> java.lang.IllegalArgumentException: No implementation of method:
>> :dosomething of protocol: #'user/Notcloseable found for class:
>> java.lang.Integer (NO_SOURCE_FILE:0)
>>
>> It _also_ for interop purposes defines a Java interface, which classes
>> that implement the protocol may or may not implement. If you use
>> extend-type, they will not. If you use deftype or defrecord to create a new
>> class that implements the protocol, it will also implement the interface.
>> This is the basis for the performance advantage of protocols.
>>
>>
>>> (defrecord Archive []
>>> java.io.Closeable
>>> (close [_] (print "closeable"))
>>>
>>> Notcloseable
>>> (dosomething [_] (print "something")))
>>>
>>>
>>> user> (def a (Archive.))
>>>
>>> user> (.close a)
>>> closeable
>>>
>>
>> Uses interop to call through the Closeable interface.
>>
>>
>>> user> (close a)
>>>
>>> Unable to resolve symbol: close in this context
>>> [Thrown class java.lang.Exception]
>>>
>>
>> No such function exists.
>>
>>
>>> user> (dosomething a)
>>> something
>>>
>>
>> Uses the protocol function, and calls the implementation you've provided.
>>
>>
>>> user> (.dosomething a)
>>> something
>>>
>>>
>> Uses the interop support to call the Protocol's interface method.
>>
>>
>>> Implementing a protol creates both java function and a clojure function.
>>> Implementing an interface only creates the java function.
>>>
>>
>> I'm not sure why you expect that implementing an interface would create a
>> function?
>>
>> --Aaron
>>
>> --
>> 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
>
>
>
--
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