Hi,
On Dec 7, 10:21 am, Tzach <
tzach.livya...@gmail.com> wrote:
> (defmulti collide-with foo)
>
> (defmethod collide-with ["asteroid" "spaceship"] (print "Boom"))
> (defmethod collide-with ["asteroid" any] (print " Wiiissh"))
> (defmethod collide-with [any "spaceship"] (print "Wooossh"))
> (defmethod collide-with [any any] (print "Dead Space"))
>
> The idea is to have a default function for a collision of asteroid
> with anything else, and of spaceship with anything else.
> What foo can I use?
Eg. a custom hierarchy. Since there is no bottom type, you have to
create it manually. Deriving Object from ::Any takes care of the Java
land. For clojure land you have to derive each type from ::Any (or
anything already deriving from ::Any).
(def space-hierarchy
(-> (make-hierarchy)
(derive Object ::Any)
(derive ::Spaceship ::Any)
(derive ::Asteroid ::Any)))
(defmulti collide-with
#(vec (map type %&))
:hierarchy #'space-hierarchy)
(defmethod collide-with :default
[_ _]
(print "Dead Space"))
(defmethod collide-with [::Asteroid ::Any]
[_ _]
(print "Wiiissh"))
(defmethod collide-with [::Any ::Spaceship]
[_ _]
(print "Wooosssh"))
(defmethod collide-with [::Asteroid ::Spaceship]
[_ _]
(println "Booom!"))
Maybe you have to disambiguate with prefer-method in certain cases. (I
don't think, that's the case here, but I haven't tested it...)
Hope this helps.
Sincerely
Meikel