
Photo by Daniil Komov on Unsplash
ClojureCLR has been around since 2009 — a full port of the Clojure compiler and runtime that targets the CLR instead of the JVM, maintained under the Clojure Core Team umbrella alongside mainline Clojure and ClojureScript. It’s still alive: it is released together with Clojure itself, and today both sit on the same tag, clojure-1.13.0-alpha8.
It exists to answer one question: I want to write Clojure, and I need .NET. JCOReflector answers a version of that same question from the opposite direction — stay on the JVM, stay on mainline Clojure, and bring .NET to you instead of porting Clojure to .NET. Worth asking plainly: for people who reach for ClojureCLR today, does JCOReflector make that unnecessary?
Two very different ways to get there
ClojureCLR reimplements the Clojure compiler itself to emit CIL instead of JVM bytecode. The payoff is genuine: .NET interop in ClojureCLR is native Clojure interop syntax, because as far as the compiler is concerned, .NET types are the only kind of host types that exist. (.ToString obj), (System.String/Format "{0}" x) — no indirection, no generated glue, it's exactly how Clojure-on-the-JVM calls Java.
The cost of that payoff is a second, parallel Clojure. Not a library — a whole separate implementation that has to track every language change mainline Clojure makes. Today the two are on the same tag, so the point is not a delay: it is that the alignment is work somebody has to keep doing, release after release. And crucially: you lose the JVM. Out of the box, a ClojureCLR program cannot reach into the Java ecosystem — Maven Central doesn’t exist from where it’s standing. The same bridge exists in the opposite direction, though: JNet generates C# bindings for JVM classes, so a .NET process can call them. Since ClojureCLR runs on a real CLR, it could in principle use JNet to reach the JVM. That route is untested here, and it means hosting a JVM next to ClojureCLR — the mirror image of what JCOReflector does.
JCOReflector doesn’t touch the compiler at all. It runs ahead of time, pointed at a .NET assembly, and generates ordinary .java source — one real class per .NET type, implementing a marker interface (IJCOBridgeReflected) and carrying everything needed to round-trip across JCOBridge, the library that actually hosts a CLR instance inside the JVM process. From Clojure’s point of view, a generated class is just another Java class: no special reader-conditional, no separate dialect, no separate compiler — ordinary :clj code, ordinary (ns ... (:import ...)), ordinary Java interop that’s been stable in Clojure since 1.0. There is one exception, generic classes, and it is the interesting part.
What that actually looks like
The generated API keeps .NET’s method names, PascalCase as in list.Add(x) or dict.TryGetValue(key, value), while properties and events follow the Java conventions: a property becomes a getX/setX pair (getCount()), an event an addX/removeX pair. Generic classes also need the anonymous-subclass idiom, new List_1() {} in Java, so that the type argument survives Java’s erasure. That idiom is the catch for Clojure: proxy, reify and gen-class generate classes, but none of them writes the generic signature of the superclass, so a generic class built with them looks like a raw one and the type argument is lost.
The project solves it with a small helper, generics. GenericSupport/new-generic, which generates that subclass at runtime. The type argument problem and the helper are described in the generics article, and the helper itself is GenericSupport.clj in the repository.
This is not an invented example: it is the test that sorts a Listwith a Comparison delegate, taken from the project’s own suite GenericComparisonDelegateSortsAList.clj, trimmed of its -main and of the imports that -main uses:
(ns generics.GenericComparisonDelegateSortsAList
(:require [generics.GenericSupport :as gs])
(:import [java.util.concurrent.atomic AtomicInteger]
[org.mases.jcobridge.netreflection IJCOBridgeReflected]
[system Comparison_1]
[system.collections.generic List_1]))
;; Exercises a generic delegate with a primitive return type (Comparison<T> returns int).
;; The delegate is a generated subclass whose Invoke is forwarded to the Clojure fn.
;; The arguments are hinted as IJCOBridgeReflected on purpose: javac picks Add(T) from the
;; generic parameter, while Clojure only sees the erased overloads and would otherwise choose
;; the more specific explicit-interface Add(...) that throws UnsupportedOperationException.
(defn- test-generic-comparison-delegate-sorts-a-list []
(let [^List_1 lst (gs/new-generic List_1 [system.Object])
calls (AtomicInteger.)
a (system.Object.)
b (system.Object.)]
(.Add lst ^IJCOBridgeReflected a)
(.Add lst ^IJCOBridgeReflected b)
(let [^Comparison_1 comparison (gs/new-generic Comparison_1 [system.Object]
{"Invoke" (fn [_ _]
(.incrementAndGet calls)
0)})]
;; Sort is selected by reflection on purpose: javac resolves Sort(Comparison_1<T>) from the
;; static type, but Clojure compiled the call against Sort(IComparer_1) (ClassCastException).
(let [sort-method (.getMethod List_1 "Sort" (into-array Class [Comparison_1]))]
(try
(.invoke sort-method lst (object-array [comparison]))
(catch java.lang.reflect.InvocationTargetException e
(throw (.getCause e)))))
(when (zero? (.get calls))
(throw (AssertionError. "Java comparison never invoked"))))))
It is honest about the price: the helper stands in for the{} idiom, and since Clojure only sees the erased overloads, the arguments need a type hint and the Sort call goes through reflection. These are mostly one-time costs: the type hints exist only at compile time, the helper generates and caches one class for each combination of type arguments, and the reflective lookup of Sort can be done once and kept for later calls (the test does it inline because it runs only once). Plain ordinary interop works for everything that is not generic; the generic parts need these few extra steps.
So — superfluous?
For the use case ClojureCLR is usually reached for — I’m writing Clojure and I need to call some .NET API — this is a genuinely strong alternative, and for a lot of people it probably removes the reason to pick ClojureCLR at all. You stay on mainline Clojure, whatever tag it is on. You keep the entire JVM ecosystem — every Maven artifact that exists, with nothing to port. And because JCOBridge hosts a real CLR inside the JVM process rather than replacing it, you can reach into Java libraries and .NET libraries from the same running program, which neither pure ClojureCLR nor pure JVM Clojure can do on its own. JNet is what would make the mirror setup possible, a ClojureCLR program calling JVM libraries, so the two directions are symmetrical: the choice is which runtime you want to be the host.
Where it doesn’t replace ClojureCLR: if the actual requirement is a native .NET deployment artifact — an assembly that drops into a pure .NET pipeline with no JVM anywhere in the picture — ClojureCLR is still doing a job JCOReflector structurally can’t, because JCOReflector’s whole premise is a JVM process at the center. And the interop itself is more ceremonious on the JCOReflector side: PascalCase generated methods, a helper in place of the {} idiom for generic classes, type hints to pick the right overload, and in the example above a call by reflection, versus ClojureCLR’s (.Method obj) being the real, unmediated thing.
Different trade-off, not a strictly worse one. But for the person whose actual goal was never “run on .NET” and was always “use some .NET library from Clojure without giving up the JVM I already depend on” — that person may not need to reach for ClojureCLR anymore.
- JCOReflector on Dev
- JCOReflector on GitHub
- ClojureCLR : clojure/clojure-clr
- JNet, the mirror project : masesgroup/JNet
- The bridge underneath : jcobridge.com
Top comments (0)