Last active
May 25, 2020 09:11
-
-
Save mping/fa9003c87796eab13790f6dc00711f14 to your computer and use it in GitHub Desktop.
Clojure hierarchy with multimethods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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