Question

I have a record like that:

(defrecord MyRec [name val])

I would like to compare instances based on the val field:

(def foo (->MyRec :foo 1))
(def bar (->MyRec :bar 2))
(> foo bar)
=> false
Was it helpful?

Solution

Firstly you should exclude clojure.core/> from ns declaration like this:

(ns foo
  (:refer-clojure :exclude [>]))

And then try this:

(defprotocol FooComparable
  (> [a b]))

(defrecord MyRec [name val]
  FooComparable
  (> [a b] (clojure.core/> (:val a) (:val b))))

(def a (MyRec. :foo 1))
(def b (MyRec. :bar 2))

(> a b) ; false
(> b a) ; true
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top