Error formatting macro: pagetree: java.lang.NullPointerException

Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.

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


  • Solve the problem, without introducing others


  • 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


  • 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:

Code Block
var(String nsName)
var(String ns, String name)
deref(String nsName)
deref(String ns, String name)
call(String varName, Object...)
call(Var var, Object...)
call(IFn fn, Object...)
eval(String code)
load(File f)

In addition, some data-related methods would be helpful:

Code Block
keyword(String nsName)
keyword(String ns, String name)
symbol(String nsName)
symbol(String ns, String name)
vector(Object... vals)
vector(java.util.Collection list)
hashmap(Object... keyvals)
hashmap(java.util.Map m)
set(Object... vals)
set(Collection vals)

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 :-(

Because clojure.lang.IFn.invoke throws Exception, 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 any call method or .invoke calls made using Var or IFn obtained through that API.

h3. Potential solutions

Wrap IFn with a delegate that catches Exception and rethrows them as RuntimeException.  An example of this is, 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 the throws Exception declaration from IFn.invoke entirely, and change function code generation to catch Exception and rethrow them as RuntimeException.  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.

Prior Discussion


2010/03/21 clojure-dev thread