liveBook · Manning

Interop

As explained in Clojure: The Essential Reference

Java interoperability is Clojure's capability to call Java classes, methods, and libraries from Clojure code.

Java interoperability, or interop, lets Clojure code use Java classes, methods, objects, and libraries. This makes Java SDK capabilities such as cryptography, networking, graphics, and math available without reimplementing them in Clojure. section 1.4 section 5.1

Choose the form that fits the operation:

  • Use `.` to access a Java method or attribute, `..` for chained calls, and `doto` for a sequence of side-effecting calls. section 21.0
  • Use `new` to construct an object. Use `try`, `catch`, `finally`, and `throw` for exception handling; `ex-info` and `ex-data` carry data with exceptions. section 21.0
  • Use `bean` for a map-like interface to an object, `clojure.reflect` to inspect classes, and array functions or macros to create and transform Java arrays. section 21.0

A Clojure entry point to a Java method

An imported Java class can be called inside a Clojure function: `(defn plus [x y] (IntegerMath/plus x y))`. In an example with overloaded `plus` methods, mapping that wrapper over numbers produces `(4 5 6)`, but calls the `Object` overload. Even converting the arguments with `int` at the caller does not change that selection through the ordinary wrapper. Defining the wrapper with `definline` instead lets a direct call expand to the Java call at the call site; with those conversions, the example selects the `int` overload. section 4.5

`definline` provides both an ordinary function and an inline expansion. Direct calls can use the expansion, allowing type information to reach the Java call; passing the function as a higher-order argument uses its ordinary function definition instead. Reserve this optimization for cases that need it: `definline` was still marked experimental in Clojure 1.10. For routine work, interop is simply a way to use the Java capability already at hand alongside Clojure’s own functions. section 4.5 section 5.1

How Clojure: The Essential Reference covers it

How deep it goes
Identifies core access forms and works through overload selection and inlining, but does not detail the Java SDK examples.
Examples
Compares ordinary and inlined wrappers around an overloaded Java addition method, including their selected overloads and numeric results.
What it assumes
Assumes familiarity with Java SDK examples while naming the Clojure forms used to access Java.
What you can do after
Apply it via Java access forms, and evaluate when an inlined Clojure wrapper improves overload selection.
How it connects
Connects Java method access to function inlining, type information, exception handling, reflection, and arrays.

Also explained in

Compare how 2 books explain interop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.