I feel like I've seen an answer to this before, but I can't find the
specific thread now.  However, the following two threads seem to have
very closely related info (including a reply or two from Rich).

http://groups.google.com/group/clojure/browse_thread/thread/a5e0ba6480d04829/e8e2a14c77f5babf?hl=en&lnk=gst&q=struct#e8e2a14c77f5babf

http://groups.google.com/group/clojure/browse_thread/thread/74d430f8e4353725/cc8ad0b22adf60cc?hl=en&lnk=gst&q=struct#cc8ad0b22adf60cc

-Jason

On Jan 21, 10:48 pm, Mark Engelberg <mark.engelb...@gmail.com> wrote:
> 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