Is there any way to determine whether something is a "struct" (versus
an ordinary hash map), and if so, determine which kind of struct it
was built from?

For example, in Scheme, consider (define-struct circle (x y radius)),
you get a circle? predicate for free that knows how to distinguish
between circle structs and other kinds of structs.  Anything like that
in Clojure?  If not, how do you discriminate between various structs
in multimethods?

In the docs for multimethods, the circle/rect example is given, and
I'm imagining it would be nice to be able to write the example like
this:
; rather than making special constructors, let's just use structs
(defstruct rect :wd :ht)
(defstruct circle :radius)

(defmulti area :struct)  ;let's pretend structs tag their structs with
some :struct keyword that gives you the struct used to build it.
(defmethod area rect [r]
    (* (:wd r) (:ht r)))
(defmethod area circle [c]
    (* (. Math PI) (* (:radius c) (:radius c))))
(defmethod area :default [x] :oops)
(def r (struct rect 4 13))
(def c (struct circle 12))

Any way to make this approach work?

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