sci

jyn 2025-09-04T02:14:45.635439Z

i want to expose a repl that has doc and source available. i've bound clojure.repl/doc successfully, but when i run it, i get this exception:

flower=> (doc vals)
-------------------------
java.lang.ClassCastException: sci.lang.Namespace cannot be cast to clojure.lang.Symbol
  [user/ <repl> 1:1]
Some details omitted; set the environment variable FLOWER_HOST_TRACE=1 for a full trackback
i think what's going on is that repl/print-doc is calling ns-name—but the host ns-name in clojure.core, not the guest ns-name in sci.impl.namespaces. do you have suggestions for what to do here? if it helps, here is the implementation of doc:
user=> (source doc)
(defmacro doc
  "Prints documentation for a var or special form given its name,
   or for a spec if given a keyword"
  {:added "1.0"}
  [name]
  (if-let [special-name ('{& fn catch try finally try} name)]
    `(#'print-doc (#'special-doc '~special-name))
    (cond
      (special-doc-map name) `(#'print-doc (#'special-doc '~name))
      (keyword? name) `(#'print-doc {:spec '~name :doc '~(spec/describe name)})
      (find-ns name) `(#'print-doc (#'namespace-doc (find-ns '~name)))
      (resolve name) `(#'print-doc (meta (var ~name))))))
i guess i can bind my own version of doc that replaces the meta on (var name) and then falls back to clojure.repl/doc?

✅ 1
borkdude 2025-09-04T11:48:00.388079Z

@jyn514 SCI already comes with a version of doc. in Clojure doc comes from clojure.repl/doc which exists in SCI: https://github.com/babashka/sci/blob/bd3555c7962d8ac5172f85c733b385b15c1e0691/src/sci/impl/namespaces.cljc#L1948

😍 1
jyn 2025-09-04T11:49:27.727609Z

oh this is beautiful, ty!

jyn 2025-09-26T14:34:44.010939Z

one more question actually -- is there way to rebind a builtin variable under a different name? normally i would use (let [sci-var] (copy-var ...) {:namespaces {'user {'doc sci-var} 'clojure.repl {'doc sci-var}}}), but i can't do that here because it's a built-in instead of something i manually call copy-var on

borkdude 2025-09-26T14:36:28.256929Z

sure, you can do this:

(def my-sci-var (sci/resolve ctx 'clojure.repl/doc))
and then add that to another ns

jyn 2025-09-26T14:39:39.996729Z

ooo beautiful, ty!

jyn 2025-09-26T14:40:21.485749Z

i think i will have to make a temporary context first and then add my-sci-var into that context, but that seems doable

borkdude 2025-09-26T14:40:36.470559Z

just (sci/init {}) will do

jyn 2025-09-26T14:41:02.107449Z

oh huh i didn't realize i could reuse variables across contexts in that way