sci 2023-09-19

Jakub Holý (HolyJak) 2023-09-19T07:00:46.730959Z

I see SCI has support for Reagent. I would like to know whether I could make #fulcro similarly compatible with SCI. What would it take & how to approach it? 🙏

Jakub Holý (HolyJak) 2023-09-22T15:38:45.258349Z

What’s the best practice for writing custom SCI configs like https://github.com/babashka/sci.configs/blob/main/src/sci/configs/reagent/reagent.cljs ? I am rewriting a rather involved https://github.com/fulcrologic/fulcro/blob/main/src/main/com/fulcrologic/fulcro/components.cljc#L1372 (or rather the fn it delegates to), which calls bunch of fns etc. Prefixing all the calls with the original ns seems as unnecessary work, if I could simply do

(ns com.fulcrologic.fulcro.components) 
(defn defsc*-for-sci [..] ...)
inside the custom settings ns. WDYT?

Jakub Holý (HolyJak) 2023-09-22T15:41:14.851269Z

Damn, it doesn’t work in cljs 😭

You could use refer perhaps

Jakub Holý (HolyJak) 2023-09-22T15:42:01.080469Z

What do you mean?

Jakub Holý (HolyJak) 2023-09-22T15:45:24.355209Z

Hm, this is turning into a nightmare. Fulcro is using plenty of #?(:clj ...) code used by its central macro, meaning that I’d need to rewrite all of it if I wanted to be able to run in SCI 😭

Yep. Perhaps the sci-macro macro could help though

Jakub Holý (HolyJak) 2023-09-22T15:47:11.521249Z

I will look into it. Though most of these are functions (called from a macro). But at least a similar approach should be possible…

The configuration has mostly to do with macros and dynamic vars. Here is the configuration for reagent: https://github.com/babashka/sci.configs/tree/main/src/sci/configs/reagent Feel free to contribute one for fulcro or let me know if you have other questions

Jakub Holý (HolyJak) 2023-09-19T08:23:10.329619Z

I saw the config but did not really understand it 😅 Is there some guide for doing this? In particular, how do I find out what is it SCI needs help with? How can I test what works/doesn’t work? I am sorry, I am completely new to SCI …

Jakub Holý (HolyJak) 2023-09-19T08:23:53.559759Z

I guess I could

(require '[sci.core :as sci])
(sci/eval-string "<some fulcro code>")
and see what breaks? I guess I’d start with sci/create-ns & friends to expose Fulcro code to SCI.

Jakub Holý (HolyJak) 2023-09-19T08:29:57.278509Z

I think I am starting to understand. I will get back here when I run into troubles.

Are you doing this for CLJS or JVM Clojure?

Jakub Holý (HolyJak) 2023-09-19T08:34:51.300899Z

cljs

right, I recommend creating a context first with (sci/init ..) and then use (sci/eval-string ctx "...")

👍 1

the challenge is mostly macros, which aren't available in cljs so you have to port them to normal functions and add ^:sci/macro metadata on those

@mhuebert actually has a macro somewhere that allows you to define a macro for both JVM Clojure, CLJS and SCI

😻 2

but this requires you to make code changes to fulcro itself

so copying the macros, changing them to regular functions and then adding ^:sci/macro metadata to it will work

Jakub Holý (HolyJak) 2023-09-19T11:05:52.607809Z

thanks a lot!

Jakub Holý (HolyJak) 2023-09-19T11:20:18.809759Z

Q: Is it possible to find out the name of the current namespace in a sci macro? When I look it &env it is just {} and thus there is no (:ns &env) . Similarly, *ns* is nil.

in JVM Clojure, &env doesn't contain the current namespace, macros in SCI work similar to JVM Clojure

whether *ns* is bound or not, depends on when the macro is invoked

let me try an example in SCI

ah you probably want to inspect sci.core/ns using (deref sci.core/ns)

you can call str on the result

and then symbol, to get the namespace's symbol

SCI does not affect the host system's *ns* - in general, it doesn't mutate the host environment

A demo:

cljs.user=> (defn ^:sci/macro foo [_ _] [(str (deref sci.core/ns)) `(str *ns*)])
#'cljs.user/foo
cljs.user=> (def ctx (sci/init {:namespaces {'foo {'foo (sci/copy-var cljs.user/foo (sci/create-ns 'foo))}}}))
#'cljs.user/ctx
cljs.user=> (sci

The foo macro both derefs the current SCI namespace at "expansion" time and at "runtime" (the last bit is evaluated after expansion)

whoops, copy/paste wasn't complete:

cljs.user=> (sci/eval-string* ctx "(foo/foo)")
["user" "user"]

👍 1

ah here was the macro to define macros for both regular clojure and SCI at the same time: https://github.com/mentat-collective/emmy/blob/e16b5692b04972f0bc9ea6d07f7ead41edccc066/src/emmy/util.cljc#L141

👀 1
Jakub Holý (HolyJak) 2023-09-19T11:44:54.630829Z

Nice! Though ☝️ wouldn’t help me, since I need to use sci.core/ns instead of (:ns &env) / *ns*

@borkdude Q about SCI 0.7.39 - SCI on cljs seems to not respect unquotes?

this is running in Clerk, via their editor example

can't reproduce:

cljs.user=> (sci/eval-string "(let [x 10] `(+ ~x ~x))")
(clojure.core/+ 10 10)

it might be that they are parsing on their own using edamame and haven't enabled :syntax-quote

or some other issue, it seems rewrite-clj is part of the mix too, I'd just submit a clerk issue

perhaps you can intercept the string that is being evaluated by the editor to see what goes in

and then print it to the console

see you at STL btw!

I see you mentioned 2.maria in your emmy announcement, has this had a proper announcement itself once? I wondered about its progress :)

haha I don’t think it has, I am leaking it 🙂

I bet you’re right that it’s an edamame thing

I love these little executable gists

I pass :all true to edamame, that should include :syntax-quote, correct?

I suspect the issue might be elsewhere, please just intercept the string that gets fed into sci/eval-string first, to determine if this is not a rewrite-clj issue which I see is also used

ah you are first parsing stuff with edamame and then eval it in SCI?

anyway, I'm not clear on what happens and short on time (packing bags). perhaps you can make smaller repros and I'll have a look again