Skip to content

Instantly share code, notes, and snippets.

@mping
Last active May 25, 2020 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mping/fa9003c87796eab13790f6dc00711f14 to your computer and use it in GitHub Desktop.
Save mping/fa9003c87796eab13790f6dc00711f14 to your computer and use it in GitHub Desktop.
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