-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
can you implement interfaces with defrecord #8
Comments
You can get from the former to the latter, but it necessitates a trip through the "interop zone" to eliminate any reasons why you'd have to fall back on You can't define interfaces using If you're looking to define an interface, look at the (undocumented) |
At the risk of seeming pre-newbie... When I try: (defrecord Rec0 [x])
(extend-type Rec0 clojure.lang.IHashEq (hasheq [this] 5)) I get an I guess what I'm saying is I can't figure out how to implement an interface with defrecord, which is the subject of the initial question. |
You're right, protocols expose an interface, but interfaces are certainly not all protocols. Protocols are a Clojure construct, not a JVM one. I'd suggest you take a look at some introductory materials (http://clojurebook.com if I may be so bold), or the standard documentation and examples that are here and there online. |
I've been scouring the standard documentation and examples. It's not bold at all to suggest your book. My investment so far have been 'The Joy of Clojure' and 'Clojure in Action'. I just got your book from O'Reilly. From the documentation and the example above, I now don't think I can implement a non-protocol JVM interface on deftype or defrecord, but the flowchart suggests that I can. The path I follow is: Am I mistaken in my understanding of the documentation, or in following the flowchart? |
Great! Your book took me directly to the solution. Add the JVM interface to the protocol with #'extend-protocol. Perhaps this could be a notation on the flowchart? |
No, you can absolutely implement any JVM interface for deftype or defrecord, just not with Extending a protocol to an interface can be questionable, and not something I want to encourage by documenting it. |
Apologies. Your book's example in "Participating in Clojure's Collection Abstractions" of Chapter 6, with (deftype ArraySet ...) is the perfect example for me. My earlier comment was made before comprehension took hold. |
Sorry if this is out of place, because I'm one of the newbies trying to use your great chart.
Should there be a path
from:
"Will the type need to extend a Java class or implement any interfaces?"
to:
"Use defrecord"
I can't figure out a way to add interfaces using defrecord. Could it be that the decision:
"Do you need to be able to refer to the class statically from Java?"
needs to be at a different point, since it steers you back to deftype/defrecord?
The text was updated successfully, but these errors were encountered: