Comments (7)
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 gen-class
.
You can't define interfaces using defrecord
; it always defines a class, which may implement Java interfaces.
If you're looking to define an interface, look at the (undocumented) definterface
. There's some usage of it here. Or, just define the interface in Java; it's really quite sane for doing that, and allows you to provide e.g. documentation for non-Clojure consumers of your interface.
from clojure-type-selection-flowchart.
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
"IllegalArgumentException infterace clojure.lang.IHashEq is not a protocol"
so it seems that while protocols create interfaces, interfaces are not necessarily protocols.
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.
from clojure-type-selection-flowchart.
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.
from clojure-type-selection-flowchart.
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:
"Will the type need to extend a Java class or implement any interfaces?" - Yes, IHashEq
"Do you need a named type or only an instance of an anonymous type?" - Named type
"Do you need to be able to refer to the class statically from Java" - No
"Is your class modeling a domain value -- thus ... semantics?" - Yes
"Use defrecord"
Am I mistaken in my understanding of the documentation, or in following the flowchart?
from clojure-type-selection-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?
from clojure-type-selection-flowchart.
No, you can absolutely implement any JVM interface for deftype or defrecord, just not with extend-protocol
. One of the links I provided earlier has examples of doing this, as does Clojure Programming.
Extending a protocol to an interface can be questionable, and not something I want to encourage by documenting it.
from clojure-type-selection-flowchart.
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.
from clojure-type-selection-flowchart.
Related Issues (1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clojure-type-selection-flowchart.