Git Product home page Git Product logo

Comments (7)

cemerick avatar cemerick commented on June 19, 2024

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.

gdavis-github avatar gdavis-github commented on June 19, 2024

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.

cemerick avatar cemerick commented on June 19, 2024

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.

gdavis-github avatar gdavis-github commented on June 19, 2024

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.

gdavis-github avatar gdavis-github commented on June 19, 2024

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.

cemerick avatar cemerick commented on June 19, 2024

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.

gdavis-github avatar gdavis-github commented on June 19, 2024

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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.