JVM: implement a host (Clojure) protocol from sci — copy-var on a protocol var (ADR 0013, half 1)

Open
#1,092 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@yogthos is already working on this.

Since Sep 16, 2026.

  • #1093 by @yogthos — open

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
clojure
Domain
devtools

Research direction

Start with doc/ai/adr/0013-jvm-native-protocols.md and trace copy-var, copy-var*, copy-ns, analyze-defrecord*, and extend-type. Follow how type-impl and .addMethod are used, then verify that copied protocol vars become protocol entries, sci implementations dispatch through the host protocol, and host classes are rejected as targets.

Written by the indexing model from the issue text.

Description

Problem

On the JVM, a Clojure protocol handed to sci (copy-var, copy-var* or copy-ns on the protocol var) is an inert copy: sci code cannot implement it, extend it, or ask satisfies? about it. On ClojureScript this works since 0.14.55 (#639, ADR 0011/0012): copy-var on a protocol produces a protocol entry and sci deftypes implement it natively. doc/ai/adr/0013-jvm-native-protocols.md sketches the JVM counterpart as "half 1" but nothing is implemented.

(defprotocol Shape (area [this]) (scaled [this k]))
(def sns (sci/create-ns 'host))
(def ctx (sci/init {:namespaces {'host {'Shape (sci/copy-var* #'Shape sns)
                                        'area (sci/copy-var* #'area sns)
                                        'scaled (sci/copy-var* #'scaled sns)}}}))
(sci/eval-string* ctx "(defrecord Sq [s] host/Shape (area [_] (* s s)) (scaled [_ k] (->Sq (* s k))))")
;; No implementation of method: :getRawRoot of protocol: #'sci.impl.vars/IVar found for class: clojure.lang.Var
(sci/eval-string* ctx "(extend-type String host/Shape (area [s] (count s)) (scaled [s k] s))")
;; No implementation of method: :getName of protocol: #'sci.impl.types/HasName found for class: nil

analyze-defrecord* alter-var-roots the protocol's :var (a host var, not a sci var) and extend-type asks HasName for the map's :ns (a host protocol has none); behind both, every implementation is registered with (.addMethod <method> <sci type> f) on what sci expects to be a multimethod dispatching on type-impl.

The embedder-side workaround is the babashka.impl.protocols pattern (a multimethod per method with a :default through the host fn, a sci protocol map naming them, the host protocol extended to SciRecord/SciType), per protocol. An application written for babashka, where defprotocol IS sci's, that shares its namespaces with extension contexts by copying every var (copy-var* over ns-interns) has its extensions' defrecords over the shared protocols fail the moment it runs on a JVM host (found porting such an app to another Clojure runtime; the JVM fails identically).

Proposal

Implement ADR 0013 half 1: copy-var/copy-var*/copy-ns on a Clojure protocol var produce a protocol entry, as on CLJS; a sci type records its implementations in its own type data; the host protocol is extended once, on first implementation, to sci's instance classes with a bridge that reads that table, so host code calling the protocol's methods on sci instances dispatches into the sci implementations. Host classes are refused as targets from sci, as on CLJS. PR to follow.

Dominant language
Clojure
Stars
1.4k
Forks
109
Avg merge
1m
Merged PRs (30d)
26

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from babashka/sci

All issues in babashka/sci

Similar issues

More Clojure issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.