Is there a way to keep ns transitions around between evaluations?
(def sci-ctx (sci/init {}))
(defn eval-form [form]
(sci/with-bindings {sci/ns @sci/ns}
(sci/eval-form sci-ctx form)))
(eval-form '(do (in-ns 'foo)
(ns-name *ns*)))
;=> foo
(eval-form '(ns-name *ns*))
;=> user ;; not foo anymore!
@denik with-bindings
implies that the binding is restored when the body has ended. If you want to implement some kind of REPL then it's best to have the loop inside the with-bindings
@borkdude I don’t have traditional REPL but more something like REP where a client sends new forms and triggers the next evaluation cycle
I tried wrapping the eval-form
function inside with-bindings but that threw errors
So you want to store the ns state per client?
Do you have multiple clients with different states?
Only one client
You could store the ns in some kind of atom and bind it the next time
(reset! the-current-ns (sci/eval-string* ctx "*ns*"))
let me try that
and then (sci/with-bindings {sci/ns (or @the-current-ns sci/ns)} ...)
nice, might have found a simpler solution:
(def sci-ctx (sci/init {}))
(def current-ns (atom @sci/ns))
(defn eval-form [form]
(sci/with-bindings {sci/ns @current-ns}
(let [ret (sci/eval-form sci-ctx form)]
(reset! current-ns @sci/ns)
ret)))
(eval-form '(do (in-ns 'foo)
(ns-name *ns*)))
;=> foo
(eval-form '(ns-name *ns*))
;=> foo
thank you @borkdude!
:thumbsup:
@denik It just occurred to me that I took the same approach here: https://github.com/babashka/xterm-sci/blob/29f2311fe5f9e9900092e44d00b357368cd5c77d/src/xterm_sci/core.cljs#L14 https://babashka.org/xterm-sci/
nice thanks for the reference