4

I have this protocol defined in namespace x:

(ns x
  (:require [..]))

(defrecord MyData [something something-else])


(defprotocol MyProtocol
  (my-fn [data]))

Now I want to create an implementation of this protocol in another namespace. I've tried doing something like this:

(ns y
  (:require [..])
  (:import (x MyData MyProtocol)))

(extend-protocol MyProtocol
  MyData
  (my-fn [data]
    ; Do something with the data
    ))

However when I try to execute my-fn like this (from my test case):

(ns y-test
  (:require [x :refer [MyData my-fn]]
            [...]))

...

(let [data (->MyData ...)]
    (my-fn data))

I get the following exception:

java.lang.IllegalArgumentException: No implementation of method: :my-fn of protocol: #'x/MyProtocol found for class: x.MyData

If I move MyProtocol to namespace y it seem to work. What am I missing?

Update

After ayato_p's answer I required the Protocol (in y) instead of importing it but I still get the same error. Moving extend-protocol from y to x resolves the problem.

1 Answer 1

2

import is just for Java classes, so you can't import MyProtocol by :import.

Following code works with your record type and protocol.

(ns y
  (:require [.. Myprotocol])
  (:import (x MyData)))
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried that now but it still gives me the same error :/
Ah, I see. Also you need to require namespace y in the y-test namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.