Hi,
I was thinking can we do some magic to easily implement single method
interfaces? What i mean how can we reduce the noise in the following:
(proxy [java.lang.Runnable] [] (run [] (println "running")))
(proxy [java.util.concurrent.Executor] [] (execute [runner] (.run
runner)))
And settled on two things:
First:
(defn extract-vector [n]
(if (= n 0)
[]
(conj
(extract-vector (- n 1))
(symbol (.concat "p" (String/valueOf n))))))
(defmacro sproxy [iface & body]
(let [clazz (resolve iface)
method (aget (.getMethods clazz) 0)
method-name (.getName method)
method-sym (symbol method-name)
params-count (alength (.getParameterTypes method))
params (extract-vector params-count)
]
`(proxy [~iface] [] (~method-sym ~params (do ~...@body)))))
This allows us to enjoy the following:
(sproxy java.lang.Runnable (println "Running"))
(sproxy java.util.concurrent.Executor (.run p1))
The disadvantage of the first approach is that it chooses the
parameter names for you (i.e. always pN)
Second try:
(defmacro sproxy2 [iface params & body]
(let [clazz (resolve iface)
method (aget (.getMethods clazz) 0)
method-sym (symbol (.getName method))
]
`(proxy [~iface] [] (~method-sym ~params (do ~...@body)))))
This allows us to write:
(sproxy2 java.lang.Runnable [] (println "Running"))
(sproxy2 java.util.concurrent.Executor [runner] (.run runner))
Is this silly? Could it be done even more generic? How can I use %
from function/lambda literal?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---