Hi Glen,
(1) Real-world example: I use polymorphism on the types of two
different arguments to define implicit conversions:
(defmulti coerce
(fn [dest-class src-inst] [dest-class (class src-inst)]))
(defmethod coerce [java.io.File String] [_ str]
(java.io.File. str))
(defmethod coerce [Boolean/TYPE String] [_ str]
(contains? #{"on" "yes" "true"} (.toLowerCase str)))
(2) Made-up, but realistic example: using polymorphism on two
different "inheritance" hierarchies on a single argument:
(defmulti service-charge (fn [acct] [(account-level acct) (:tag acct)]))
(defmethod service-charge [::acc/Basic ::acc/Checking] [_] 25)
(defmethod service-charge [::acc/Basic ::acc/Savings] [_] 10)
(defmethod service-charge [::acc/Premium ::acc/Account] [_] 0)
(3) Example from inside Clojure itself: using external tags to layer a
hierarchy onto existing objects without their knowledge (inspector.clj).
All of these are documented in the book [1] and you can view the
sample code at [2].
Cheers,
Stu
[1] http://www.pragprog.com/titles/shcloj/programming-clojure
[2] http://github.com/stuarthalloway/programming-clojure/. See
examples/multimethods*
> I'm used to polymorphism in OO systems where everything in driven
> from inheritance hierarchy. Clojures defmulti style polymorphism
> seems powerful but has left me wondering how to most effectively use
> it. I'm looking for some good real world examples of how people
> have used polymorphism in clojure.
>
> Regards,
>
> Glen Stampoultzis
>
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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
-~----------~----~----~----~------~----~------~--~---