How about:
(defmulti length (fn [x]
(if (= :stateMachine (:class x))
(:state x)
(:class x))))
(defmethod length :yardstick [x] 36)
(defmethod length :walking [x] "short")
(defmethod length :running [x] "long")
user=> (length {:class :yardstick})
36
user=> (length {:class :stateMachine :state :walking})
"short"
user=> (length {:class :stateMachine :state :running})
"long"
It would probably be better to have the fn return a vector so you
don't have to worry about :state and :class values with colliding
names, but that's the basic idea.
Cheers,
Stuart
In general, if you have a conditional or other enumeration of things
in your dispatch method it is a warning sign that you might not be
getting the leverage out of multimethods - their prime reason to exist
is to provide open, extensible case logic.
Never say never, but in this case it's better to use two multimethods.
Rich
I have heard it claimed (on one of the Scala lists, I think) that
patterns themselves are an anti-pattern...
Randall Schulz