Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suitable Java type for a Clojure function which takes an argument?

Tags:

java

clojure

I'm writing a Java class (it's a GUI widget) which will be used from Clojure. When the Java class is initialized, it must be provided a callback function. When certain conditions are met, the Java class will call the function with a single argument.

I'm trying to figure out the type which the callback function should be declared as in the Java code.

I know that Clojure functions implement Runnable and Callable, but neither Runnables nor Callables can take an argument when invoked. Of course, Clojure functions also implement IFn, but I would prefer to use a standard Java type. Any ideas?

like image 922
Alex D Avatar asked Jun 06 '26 17:06

Alex D


1 Answers

I checked all the interfaces implemented by Clojure functions using:

(ancestors (class (fn [a] a)))

And got the answer:

#{clojure.lang.AFunction java.lang.Object clojure.lang.Fn clojure.lang.IFn clojure.lang.IObj java.io.Serializable java.util.concurrent.Callable java.lang.Runnable java.util.Comparator clojure.lang.AFn clojure.lang.IMeta}

Drat! Looks like I won't do better than to use IFn.

It's strange to think that after almost 20 years of history, the Java platform has no standard class or interface for a lambda-like object! There are ActionListeners and this-listeners and that-listeners, Runnable and Callable, but no general purpose "function" objects.

like image 51
Alex D Avatar answered Jun 09 '26 07:06

Alex D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!