On Mar 14, 8:15 pm, Daniel Solano Gomez <cloj...@sattvik.com> wrote:
> I believe there are two approaches to doing this in Clojure:
>
> 1. Multimethods:http://clojure.org/multimethods
> 2. Protocols:http://clojure.org/Protocols
>
> Of the two, as of Clojure 1.2, protocols are the preferred way of doing
> things.  

What? Who says they're preferred? Prefer the one that is simplest,
unless you really need performance, in which case use protocols.

> They handle the simplest and most common case of polymorphism: by type.

;; snip protocol suggestions, which were fine


I might be inclined to do this as a multimethod dispatching on the
number of points in the shape. Maybe I represent my shape as a vector:
[[p1, p2, ...] more-data]. Then I can write:

(defmulti draw (fn [pts & more] (count pts))

(defmethod draw 1 [[center] radius] (do stuff with these))
(defmethod draw 2 [[p1 p2]] (draw a line))
(defmethod draw 3 [[p1 p2 p3] filled?] (draw a triangle)) ...
(defmethod draw :default [points] (connect up all the points))

In fact a lot of the methods won't need to even be written,
because :default can handle them by just drawing lines from point to
point.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to