Self-referential proxies

20 views
Skip to first unread message

Kevin

unread,
Sep 27, 2008, 11:36:23 AM9/27/08
to Clojure
Hello,

I had some difficulty writing the following java code in Clojure:

Runnable runnable = new Runnable() {
public void run() {
animate();
display.timerExec(TIMER_INTERVAL, this);
}
};
display.timerExec(TIMER_INTERVAL, runnable);


For the moment I have worked around the 'this' reference using refs.
Is there a cleaner way of doing this? If not a 'this' function/symbol
might be useful for the proxy macro.

This is roughly how I wrote it:

(let [runnable (ref nil)] (dosync (ref-set runnable (proxy [Runnable]
[] (run [] (animate) (dosync ... @runnable ...

Chouser

unread,
Sep 27, 2008, 4:06:40 PM9/27/08
to clo...@googlegroups.com
On Sat, Sep 27, 2008 at 11:36 AM, Kevin <conf...@gmail.com> wrote:
>
> I had some difficulty writing the following java code in Clojure:
>
> Runnable runnable = new Runnable() {
> public void run() {
> animate();
> display.timerExec(TIMER_INTERVAL, this);
> }
> };
> display.timerExec(TIMER_INTERVAL, runnable);

Clojure functions implement Runnable, so you ought to be able to do
something like:

(defn my-run []
(animate)
(.timerExec display *timer-interval* my-run))
(.timerExec display *timer-interval* my-run)

That's untested. Good luck. :-)
--Chouser

Kevin

unread,
Sep 27, 2008, 5:32:24 PM9/27/08
to Clojure
Thanks, that's handy to know and a lot cleaner.
Reply all
Reply to author
Forward
0 new messages