Very cool.

On Wed, Apr 1, 2009 at 8:47 AM, Rich Hickey <richhic...@gmail.com> wrote:
>
>
> I've added get-method (SVN 1338).
>
> (derive ::Circle ::Shape)
> (derive ::Rect ::Shape)
>
> (defmulti area :Shape)
>
> ;note - you can name methods
> (defmethod area ::Shape area-shape [x] nil)
>
> (get-method area ::Rect)
> #<user$area_shape__43 user$area_shape_...@674a93a6>
>
> (defmethod area ::Rect area-rect [r]
>    (* (:wd r) (:ht r)))
> (defmethod area ::Circle area-circ [c]
>    (* (. Math PI) (* (:radius c) (:radius c))))
>
> (get-method area ::Rect)
> #<user$area_rect__18 user$area_rect_...@4e42751f>
>
> (get-method area ::Circ) ;not there
> nil
>
> (get-method area ::Circle)
> #<user$area_circ__20 user$area_circ_...@74da0c91>
>
> ;if you don't think squares are rectangles, you can still reuse
> implementation (silly example)
> (defmethod area ::Square area-square [sqr]
>  ((get-method area ::Rect) {:wd (:dim sqr) :ht (:dim sqr)}))
>
> (area {:Shape ::Square :dim 20})
> 400
>
> Note how you can name methods for diagnostic purposes. This doesn't
> introduce names into the namespace, just puts a name on the fn object.
>
> Rich
>
> >
>

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