JVM: implement a host (Clojure) protocol from sci — copy-var on a protocol var (ADR 0013, half 1)
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 45/100
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from babashka/sci
-
Difficulty 3/5 1-2 days Newbie friendliness 65/100
babashka/sci#1097 · 3 comments ·
Maintainers usually reply within 1 day
-
Explore forkable SCI runtime worldsMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 5/5 Over a week Newbie friendliness 20/100
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
babashka/sci#1040 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 52/100
babashka/sci#1002 · 1 comment ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
nextjournal/clerk#830 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
IOP tokens release blocker team qa team qa autotests WG adoption
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
penpot/penpot#13540 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
izhangzhihao/intellij-rainbow-brackets#2903 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100