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.com
http://www.michaelnygard.com/

Release It! Design and Deploy Production-Ready Software
http://bit.ly/ReleaseIt

Beautiful Architecture
http://bit.ly/BeautifulArchitecture

97 Things Every Software Architect Should Know
http://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