Extending IFn

2011-10-27 Thread Sean Devlin
I'm experimenting with creating my own fn types.  I was wondering if
there was a better way of extending IFn than this:

https://gist.github.com/1321330

Is there a more idiomatic way?

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


Re: Extending IFn

2011-10-28 Thread Meikel Brandmeyer (kotarak)
Hi,

since IFn is a Java interface and not a protocol, I'm afraid there is no 
better way to define this. However I would write the macro slightly 
differently. I find this more approachable. YMMV.

(def max-arities 20)

(defmacro definvokable
  [type fields & deftype-tail]
  (let [f(fields 0)
args (repeatedly max-arities gensym)
arity(fn [n]
   (let [args (take n args)]
 `(invoke [this# ~@args] ((. this# ~f) ~@args
vararg   `(invoke [this# ~@args more#]
(apply (. this# ~f) ~@args more#))
apply-to `(applyTo [this# args#] (apply (. this# ~f) args#))]
`(deftype ~type
   ~fields
   clojure.lang.IFn
   ~@(map arity (range (inc max-arities)))
   ~vararg
   ~apply-to
   ~@deftype-tail)))

Sincerely
Meikel

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

Re: Extending IFn

2011-10-28 Thread Sean Devlin
Eh, I was afraid the interface would be a problem.  So there isn't a
good way to reuse interface implementations for interacting w/ legacy
Java quite yet.

I do like your approach to the macro better.  As usual you deliver,
Meikel :)

Sean

On Oct 28, 3:11 am, "Meikel Brandmeyer (kotarak)" 
wrote:
> Hi,
>
> since IFn is a Java interface and not a protocol, I'm afraid there is no
> better way to define this. However I would write the macro slightly
> differently. I find this more approachable. YMMV.
>
> (def max-arities 20)
>
> (defmacro definvokable
>   [type fields & deftype-tail]
>   (let [f        (fields 0)
>         args     (repeatedly max-arities gensym)
>         arity    (fn [n]
>                    (let [args (take n args)]
>                      `(invoke [this# ~@args] ((. this# ~f) ~@args
>         vararg   `(invoke [this# ~@args more#]
>                     (apply (. this# ~f) ~@args more#))
>         apply-to `(applyTo [this# args#] (apply (. this# ~f) args#))]
>     `(deftype ~type
>        ~fields
>        clojure.lang.IFn
>        ~@(map arity (range (inc max-arities)))
>        ~vararg
>        ~apply-to
>        ~@deftype-tail)))
>
> Sincerely
> Meikel

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