Welcome! Please see the About page for a little more info on how this works.

0 votes
in Protocols by

`
(defprotocol TestProtocol
(tester [o]))

(let [t tester]
(defn another-tester [o]
(t o)))

(def another-tester2 tester)

(extend-protocol TestProtocol
String
(tester [o] (println "Strings work!")))

(another-tester "A") ;; Error
(another-tester2 "A") ;; Error
(tester "A") ;; Works fine

(let [t tester]
(defn another-tester [o]
(t o)))

(another-tester "A") ;; Works fine

(def another-tester2 tester)

(another-tester2 "A") ;; Works fine

(extend-protocol TestProtocol
Long
(tester [o] (println "Longs work!")))

(another-tester "A") ;; Works fine
(another-tester 3) ;; Error
(another-tester2 3) ;; Error
`

5 Answers

0 votes
by
_Comment made by: nathanmarz_

This issue appears to be Clojure specific – I did some testing in CLJS and was unable to reproduce the issue.
0 votes
by

Comment made by: gshayban

Nathan,
Not sure if you tried this, but using:

`(def another-handle #'the-protocol-function)`
rather than dereffing outright.

0 votes
by
_Comment made by: nathanmarz_

That's a good workaround but it does seem that my test case should work. I ran into this because I was passing around functions dynamically and saving them for later execution – and this issue popped up with protocol methods. Having to pass around protocol methods differently than regular functions doesn't seem right.
0 votes
by

Comment made by: hiredman

this is a result of the protocol implementation in clojure, protocol extension mutates the vars, once you have taken then value of the var (which happens once for top level forms) you will not see further mutations of the var so no more protocol extension

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-1796 (reported by alex+import)
...