Suppose I have a closed world of valid dispatch keys; in my concrete example, it's the type of nybbles. There are two obvious ways to define an operation of some parameters that behaves differently based on a nybble argument:
Using
case
, e.g.:(defn read-arg [arg-mode mem cursor] (case arg-mode 0x0 [:imm 0] 0x1 [:imm (read-fwd peek-word8 mem cursor)] ;; And so on 0xf [:ram (read-fwd peek-word32 mem cursor)]))
Using
defmulti
/defmethod
:(defmulti read-arg (fn [arg-mode mem cursor] arg-mode)) (defmethod read-arg 0x0 [_ mem cursor] [:imm 0]) (defmethod read-arg 0x1 [_ mem cursor] [:imm (read-fwd peek-word8 mem cursor)]) ;; And so on (defmethod read-arg 0xf [_ mem cursor] [:ram (read-fwd peek-word32 mem cursor)])
Which is considered nicer style for Clojure? Would the answer be different if the dispatch was done on symbols instead of nybbles?