liveBook · Manning

Definline

As explained in Clojure: The Essential Reference

definline is a Clojure macro that defines a function eligible for compiler inlining, including for Java interoperability.

`definline` defines a Clojure function with an additional version that the compiler can substitute at direct call sites. It is useful for small, performance-sensitive wrappers around Java calls: substitution lets the caller’s type information reach the interop expression. Use it when the inline expression and ordinary function body are intended to do the same job. section 4.5 section 2.1.1

One definition, two forms

```clojure (definline timespi [x] `(* ~x 3.14)) (timespi 3) ;; 9.42 ``` Macro-expanding the declaration reveals a `defn` plus `:inline` metadata containing the substitution function. The backquoted expression supplies the inline form without requiring a separately written `:inline` implementation. section 4.5

Invocation matters. A direct call can use the inline form; passing the function as a value, as in `(map sq xs)`, uses the ordinary function. Keep both behaviors aligned. If an inline expression refers to an argument more than once, bind it first: `(let [x# ~x] (* x# x#))` evaluates the supplied expression once. section 4.5

public class IntegerMath { ; #1

  public static int plus(Object op1, Object op2) {
    System.out.println("int plus(Object Object)");
    return ((Number) op1).intValue() + ((Number) op2).intValue();
  }

  public static int plus(Integer op1, Integer op2) {
    System.out.println("int plus(Integer Integer)");
    return op1 + op2;
  }

  public static int plus(int op1, int op2) {
    System.out.println("int plus(int int)");
    return op1 + op2;
  }

  //[...] other types
}

The overloaded `IntegerMath/plus` methods make overload selection visible: each prints the parameter types selected for that call. section 4.5

Keep caller types visible to Java

A normal `(defn plus [x y] (IntegerMath/plus x y))` selects `plus(Object, Object)` in the shown `map` example—even when the caller applies `int` to both arguments. Replacing it with `(definline plus [x y] `(IntegerMath/plus ~x ~y))` makes those direct calls select `plus(int, int)` instead; both versions produce `(4 5 6)` for the demonstrated inputs. section 4.5

`definline` is experimental, not a general replacement for `defn`. Prefer an ordinary function unless inlining addresses a concrete interop need; earlier Clojure releases also had a serious AOT-compilation issue. section 4.5 section 2.1.1

How Clojure: The Essential Reference covers it

How deep it goes
Explains expansion into `defn` and `:inline` metadata, then contrasts direct calls with higher-order use through worked examples.
Examples
Uses a multiplication function, an overloaded Java addition method, and a squared-value function to expose call-site differences.
What it assumes
Assumes familiarity with Clojure functions, macro syntax, and Java method calls; demonstrates expansion rather than teaching those foundations.
What you can do after
Apply it via `definline` to wrap a Java method when direct-call inlining improves overload selection.
How it connects
Links function inlining to `defn`, `defmacro`, `:inline` metadata, Java interoperability, and compiler optimization.

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.