On Mar 6, 4:37 pm, Stuart Sierra <
the.stuart.sie...@gmail.com> wrote:
> Hello all,
> Given that Clojure fns are Callable but not Runnable, i.e.:
>
> user=> (instance? java.util.concurrent.Callable (fn []))
> true
> user=> (instance? java.lang.Runnable (fn []))
> false
>
> Is there an efficient way to use a Clojure fn as a Runnable? I need a
> Runnable so I can use
> ScheduledExecutorService.scheduleWithFixedDelay(). The best I could
> come up with is:
>
> (proxy [Runnable] [] (run [] (my-function... )))
>
> But I feel like a proxy shouldn't be necessary.
>
There's nothing wrong/inefficient with a proxy - it's much like using
an anonymous class to implement Runnable in Java, except the proxy
class will be created only once, cached, and reused. You shouldn't
hesitate at all to do this - might be a candidate for a library
function/macro.
> I assume fn's cannot implement both Callable and Runnable, because a
> lot of Executor methods are overloaded for both.
>
Right, it can be awkward to implement both overloading-wise, so I
chose to implement the more recent solution.
Rich