Hi Patrick,
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
> Is there anyway to do the following with the existing multi-method
> facilities?
>
> There is a general multi-method length(object) that splits off to
> different methods depending on the :class of the object.
>
> And then specifically for objects with :class = :stateMachine, I want
> to define it's method to split off further depending on the :state of
> the object.
>
> (defmulti length :class)
> (defmethod-multi length :stateMachine :state)
>
> (defmethod length :stateMachine :walking
> (println "Short distance"))
>
> (defmethod length :stateMachine :running
> (println "Long distance"))
>
> Thanks very much for your help.
> -Patrick
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---