Key

Problem Statement

People looking to call Clojure code from Java are faced with a large number of Java classes from the Clojure implementation and don't know which ones are stable and safe to use.

Other problems, not in scope

people don't like to type

why are they using Java then?

people don't understand Clojure, but still want to leverage it from Java

Clojure is not a Java library

People can and do write libraries in Clojure for consumption from Java, but each must define its own Java API

Mission

Solve the problem, without introducing others

Proposal

provide the smallest possible API, in a single class, to allow people to find and call Clojure fns

a single thing to refer people to

opens up entire API

everything in Clojure is exposed via fns

if something critical is missing, let's add a fn rather than grow this API

no wrappers that need to be maintained

no semantic or other mismatch with original fns

docs are Clojure docs

Issues

We want to give people access to Var objects but not have them use the Var class

too much other stuff there, plus it's concrete

the interfaces they need are IFn and IDeref

but no interface unifies them, so we have no good type to use

make one?

bindings are also tricky to use from Java

can we function-ify them?

with-bindings (fn!)

with-repl-bindings

with-compilation-bindings

Stable Public Java API

Clojure does not currently provide a stable public Java API. This makes calling Clojure code from Java more verbose and more complicated than it needs to be, and requires developers to refer to classes that are actually just details of Clojure's current implementation that can and probably will change in the future.

A public Java API probably only needs to be a single class providing a small set of static utility methods, primarily intended to smooth the path for calling Clojure functions from Java. Off the top of my head, this might be an initial set of such methods:

As long as we are careful with our naming, all of these methods could be statically imported in Java files, making a lot of common interop tasks much less verbose.

IFn.invoke throws Exception :-(

Becauseclojure.lang.IFn.invokethrowsException, calling Clojure functions in Java (via IFn or resolved vars) implies a lot of checked exception pain. Without some solution to this, the public API for Java would also bubble up that pain through anycallmethod or.invokecalls made usingVarorIFnobtained through that API.

h3. Potential solutions

WrapIFnwith a delegate that catchesExceptionand rethrows them asRuntimeException. An example of this isSafeFn.java, which allows nREPL connection functions to be used from Java with a minimum of exception management pain. The public Java API would presumably perform this wrapping automatically when making calls or when returning a function or var that will be {{.invoke}}d later.

Alternatively, drop thethrows Exceptiondeclaration fromIFn.invokeentirely, and change function code generation to catchExceptionand rethrow them asRuntimeException. That would simplify the Java interaction, not force the introduction of one or two new classes, and avoid any wrapping inefficiencies, but I don't yet know what the repercussions would be to the compiler to effect this change or what the perf impact would be (either at runtime or compile/load time given the additional heft of each new function class).

RH - or drop the declaration and don't wrap. Checked exceptions are a fiction of javac, and not a fiction of Clojure. Implemented.