sci

https://github.com/babashka/SCI - also see #babashka and #nbb
borkdude 2020-09-16T12:22:30.062600Z

IDeref / IAtom now implementable in user code on sci master: https://twitter.com/borkdude/status/1306206133539999745

2020-09-16T16:29:56.066100Z

Hi I have a little question regarding eval-form. It's said to bind sci/ns with sci/binding . I have try the following which works:

(def program
  '(let [current-ns (-> *ns* str symbol)]
     (ns foo)
     (defn bar [x] (inc x))
     (in-ns current-ns)

     (require 'foo)
     (foo/bar 1)))


(def env (sci/init {}))

(sci/binding [sci/ns sci/ns]
  (sci/eval-form env program))
Is this the proper use of the binding requirement? sci.impl.interpreter/eval-string* goes with something like:
(vars/with-bindings {vars/current-ns @vars/current-ns}

borkdude 2020-09-16T16:34:54.066700Z

@jeremys (sci/binding [sci/ns @sci/ns] ...)

borkdude 2020-09-16T16:35:42.067Z

so: bind sci/ns to the current value of sci/ns

borkdude 2020-09-16T16:36:22.067400Z

sci/ns is a sci var, not a normal var, that's why you have to deref is explicitly

borkdude 2020-09-16T16:36:50.067900Z

the reason for the binding is the same as in clojure: set! is only allowed when there is already a thread-local binding

borkdude 2020-09-16T16:48:12.068500Z

@jeremys ns switches aren't supported in let forms, only top level or in top-level do:

(require '[sci.core :as sci])

(def program
  '(do (ns foo)
       (defn bar [x] (inc x))
       (ns user)
       (require 'foo)
       (foo/bar 1)))

(def env (sci/init {}))

(sci/binding [sci/ns sci/ns]
  (prn (sci/eval-form env program)))

borkdude 2020-09-16T16:49:51.068900Z

Note that that doesn't work in normal Clojure either:

(let [current-ns (-> *ns* str symbol)]
  (prn current-ns)
  (ns foo)
  (defn bar [x] (inc x))
  (in-ns current-ns)
  (require 'foo)
  ((resolve 'foo/bar) 1))

borkdude 2020-09-16T16:52:30.069200Z

but this on the other hand does work:

(do
  (ns foo)
  (defn bar [x] (inc x))
  (in-ns 'user)
  (require 'foo)
  (prn ((resolve 'foo/bar) 1)))

2020-09-16T17:36:06.072700Z

Ok thanks! Having reified environments, eval, macros is pretty powerful but sometimes it is confusing, especially playing with sci witch adds another layer of environment, eval and macros... It's fun though!