Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To test if something satisfies a protocol, there's <a href="http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/satisfies?" rel="noreferrer"><code>satisfies?</code></a>.</p> <h3>Edit:</h3> <p>Protocols and datatypes are too new in Clojure (and still evolving fast) for me to remark much about what's idiomatic or not. But you should note that <code>defrecord</code> already implements type-and-value-based equality. Unless you really need a custom hashcode for your objects, you could consider using <code>defrecord</code>.</p> <pre><code>(defrecord Point [#^Integer x #^Integer y] Location (get-x [p] x) (get-y [p] y) (add [p q] (let [x2 (get-x q) y2 (get-y q)] (Point. (+ x x2) (+ y y2))))) user&gt; (= (Point. 1 2) {:x 1 :y 2}) false user&gt; (= (Point. 1 2) (Point. 1 2)) true </code></pre> <p>You also get the added bonus of being able to access your fields via keyword lookup, and being able to put metadata on your objects, which <code>defrecord</code> gives you for free.</p> <pre><code>user&gt; (:x (Point. 1 2)) 1 </code></pre> <p>It's possible that <code>defrecord</code>-defined things will have custom reader syntax someday in Clojure, so they can be printed readably and read back in with the Clojure reader. Unless you're really attached to your version of <code>toString</code>, you might keep this in mind as well. Right now, records already print human-readably if not machine-readably.</p> <pre><code>user&gt; (Point. 1 2) #:user.Point{:x 1, :y 2} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload