If the set of types is closed and will not be extended by users, there's
nothing wrong with just writing your own dispatch using cond, something
like:

(defn draw [shape]
  (cond
     (triangle? shape) (draw-triangle shape)
     (circle? shape) (draw-circle shape)
      ...))

Then just write your helper functions: triangle?, draw-triangle, circle?,
draw-circle and you're good to go.  Clearly, no matter what, you need *some*
way to distinguish between your different types.  It doesn't necessarily
have to be a true "type slot", as provided by defrecord.  It could be that
you represent circles, for example, as a map that begins {:shape :circle
...}.

But it's true that protocols or multimethods would be the more common way to
do this.  Protocols are reportedly faster, but not as flexible (e.g., *must*
dispatch on type, such as that provided by defrecord, no inheritance,
etc.).  I still tend to go with multimethods.  Multimethods (or rolling your
own predicates and dispatch as I described above) would allow you to use the
combination of vectors and structs you outlined.

-- 
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