-
-
Notifications
You must be signed in to change notification settings - Fork 54
Open
Labels
enhancementImprovement to an existing featureImprovement to an existing feature
Description
I've been trying for some time to get CIDER's inspector to display my custom
data types correctly and after poking around in the internals I think I
have finally narrowed the issue down to orchard's printing.
The dispatch function in question (attached below) makes it effectively
impossible to overwrite orchard's default methods. If x happens to be
a instance of Map, for example, then it will always dispatch with the
:map value, even if the user has also defined a method matching (type x):
(defmulti print
(fn [x _]
(cond
(nil? x) nil
(instance? String x) :string
(instance? Number x) :scalar
(instance? Keyword x) :scalar
(instance? Symbol x) :scalar
(instance? Map x) :map
(instance? IPersistentVector x) :vector
(instance? List x) :list
(instance? IPersistentSet x) :set
(instance? Eduction x) :list
(instance? Var x) :default
(.isArray (class x)) :array
:else (type x))))
This is in direct opposition to Clojure's own print-method:
(defmulti print-method (fn [x writer]
(let [t (get (meta x) :type)]
(if (keyword? t) t (class x)))))
This not only gives preference to the user's own type tags,
but it also dispatchs on the actual classes rather than
preset keywords.
A short example:
(defrecord rec [x])
(defmethod print-method rec
[x w]
(print-method "hello" w))
(print-str (rec. 1)) ;; => "hello"
(defmethod orchard.print/print rec
[x w]
(orchard.print/print "hello" w))
(orchard.print/print-str (rec. 1)) ;; => "{ :x 1 }"
(defmethod orchard.print/print ::rec
[x w]
(orchard.print/print "hello" w))
(orchard.print/print-str (with-meta (rec. 1) {:type ::rec})) ;; => "{ :x 1 }"
Metadata
Metadata
Assignees
Labels
enhancementImprovement to an existing featureImprovement to an existing feature