Skip to content

Instantly share code, notes, and snippets.

@mping
Last active May 25, 2020
Embed
What would you like to do?
Clojure hierarchy with multimethods
(defn dispatch-fn [o]
(println (vals (select-keys o [:type :entity])))
[(or (get o :type) ::any)
(or (get o :entity) ::any)])
(def hiera (atom (-> (make-hierarchy)
(derive :string ::any)
(derive :object ::any))))
(ns-unmap *ns* 'mm)
(defmulti mm dispatch-fn :hierarchy hiera)
(defmethod mm [:object :number] [o] "OBJ number")
(defmethod mm [:object :string] [o] "OBJ STR")
(defmethod mm [:object ::any] [o] "OBJ ANY")
(defmethod mm :default [o] "DEFAULT")
(mm {:type :object :entity nil}) ;; OBJ ANY
(comment
(isa? @hiera :object ::any))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment