Fork me on GitHub
#sci
<
2023-09-19
>
Jakub Holý (HolyJak)07:09:46

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

borkdude07:09:20

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)08:09:10

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)08:09:53

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)08:09:57

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

borkdude08:09:42

Are you doing this for CLJS or JVM Clojure?

borkdude08:09:59

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

👍 1
borkdude08:09:27

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

borkdude08:09:19

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

😻 2
borkdude08:09:28

but this requires you to make code changes to fulcro itself

borkdude08:09:08

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

Jakub Holý (HolyJak)11:09:18

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.

borkdude11:09:19

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

borkdude11:09:33

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

borkdude11:09:52

let me try an example in SCI

borkdude11:09:56

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

borkdude11:09:03

you can call str on the result

borkdude11:09:16

and then symbol, to get the namespace's symbol

borkdude11:09:36

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

borkdude11:09:12

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

borkdude11:09:13

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

borkdude11:09:40

whoops, copy/paste wasn't complete:

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

👍 1
borkdude11:09:27

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)11:09:54

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

Jakub Holý (HolyJak)15:09:45

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)15:09:14

Damn, it doesn’t work in cljs 😭

borkdude15:09:47

You could use refer perhaps

Jakub Holý (HolyJak)15:09:24

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 😭

borkdude15:09:21

Yep. Perhaps the sci-macro macro could help though

Jakub Holý (HolyJak)15:09:11

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

Sam Ritchie15:09:54

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

Sam Ritchie15:09:13

this is running in Clerk, via their editor example

borkdude15:09:25

can't reproduce:

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

borkdude15:09:40

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

borkdude16:09:45

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

borkdude16:09:18

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

borkdude16:09:24

and then print it to the console

borkdude16:09:00

see you at STL btw!

borkdude16:09:25

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

Sam Ritchie16:09:46

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

Sam Ritchie16:09:01

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

Sam Ritchie16:09:03

I love these little executable gists

mkvlr19:09:45

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

borkdude19:09:32

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

borkdude19:09:28

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

borkdude19:09:52

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

Jakub Holý (HolyJak)15:09:45

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?