Most notably, reify does not "def" anything.  It's very in-tune with
functional programming in that way, as it has no side-effects, whereas
defrecord adds a class to the namespace.

reify has been compared to Java's anonymous classes.  A good example
of its use might be in implementing a factory method:

(defn make-listener [name]
  (reify java.awt.event.ActionListener
    (actionPerformed [listener action-event]
      (prn name action-event))))

(actionPerformed (make-listener "my-listener") some-action-event)
"my-listener" #<ActionEvent ...>

Note that the object created with reify captures the "name" argument
passed to the factory function.

Ryan

On Jun 15, 6:59 pm, Michael Nygard <mtnyg...@gmail.com> wrote:
> You could also use reify:
>
> (defn make-foo [s]
>   (reify clojure.lang.IFn
>     (invoke [this] (str "Hello, " s))))
>
> ((make-foo "RJ"))
> "Hello, RJ"
>
> I have to admit, though, that I'm unclear on the relative merits of defrecord 
> vs. reify. Anyone want to comment?
>
> Cheers,
> -Michael Nygard
>
> On Jun 15, 2011, at 12:57 PM, Ken Wesson wrote:
>
>
>
>
>
>
>
>
>
> > On Wed, Jun 15, 2011 at 3:53 PM, RJ Nowling <rnowl...@gmail.com> wrote:
> >> Hi,
>
> >> I'm sorry if this has been asked before, but I would like to know how
> >> to create data structures in Clojure that can be used in the same way
> >> as the built-in data structures.  For example, I can access the
> >> elements of a vector by (my-vec 1).  How can I implement this
> >> interface when creating a data structure in Clojure?
>
> > (defrecord Foo [...]
> >  ...
> >  IFn
> >  (invoke [this] (do-this-on-zero-argument-call))
> >  (invoke [this x] (do-when-called-with-x))
> >  (invoke [this x y] (+ x y)))
>
> > => ((Foo.) 33 9)
> > 42
> > =>
>
> > --
> > Protege: What is this seething mass of parentheses?!
> > Master: Your father's Lisp REPL. This is the language of a true
> > hacker. Not as clumsy or random as C++; a language for a more
> > civilized age.
>
> > --
> > 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
>
> Michael T. Nygard
> mtnyg...@gmail.comhttp://www.michaelnygard.com/
>
> Release It! Design and Deploy Production-Ready Softwarehttp://bit.ly/ReleaseIt
>
> Beautiful Architecturehttp://bit.ly/BeautifulArchitecture
>
> 97 Things Every Software Architect Should Knowhttp://bit.ly/97Things

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