Clojure multimethod giving unexpected null pointer -
i'm having hard time getting multimethods in clojure work expect. distillation of code follows.
(defn commandtype [_ command] (:command-type command)) (defmulti testmulti commandtype) (defmethod testmulti :one [game command] (str "blah")) (defmethod testmulti :default [& args] "cannot understand") (testmulti "something" {:command-type :one}) (commandtype "something" {:command-type :one})
now expect here have method commandtype called on arguments of course return :one should send first defmethod instead null pointer exception. simplest invocation of multimethod come gives me null pointer:
(defmulti simplemulti :key) (defmethod simplemulti "basic" [params] "basic value") (simplemulti {:key "basic"})
and yet example in clojure docs located here works fine. there fundamental i'm doing wrong?
so far can see, works.
given
(defmulti testmulti (fn [_ command] (:command-type command))) (defmethod testmulti :one [game command] (str "blah")) (defmethod testmulti :default [& args] "cannot understand")
then
(testmulti "something" {:command-type :one}) ; "blah" (testmulti "something" {:command-type :two}) ; "cannot understand" (testmulti "something" 5) ; "cannot understand"
as expected.
i reset repl before running above afresh.
and simple example works too. given
(defmulti simplemulti :key) (defmethod simplemulti "basic" [params] "basic value")
then
(simplemulti {:key "basic"}) ; "basic value"
Comments
Post a Comment