This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-07-19
Channels
- # announcements (5)
- # beginners (36)
- # calva (2)
- # cider (26)
- # cljs-dev (2)
- # clojure (121)
- # clojure-spec (2)
- # clojure-uk (2)
- # clojurescript (12)
- # component (1)
- # conjure (6)
- # datomic (9)
- # docker (1)
- # fulcro (15)
- # lambdaisland (1)
- # malli (5)
- # meander (24)
- # off-topic (5)
- # re-frame (5)
- # reagent (3)
- # ring (3)
- # shadow-cljs (41)
- # sql (4)
- # vim (3)
- # xtdb (7)
Hi everyone! I have a question about cljs/eval.. if I run eval on a "defn f ..." in my web app, and then eval the expr "(f ...)" it works! So it is saved somewhere. I thought maybe in the cljs/state object but I pass an empty one in each time. Also interestingly, the console logs a warning "undeclared Var cljs.user/f" but it still works. So my question is - where is the cross-call data being stored? How is it possible to invoke f on a separate call? And can I access this state?
I think it's just using the cljs.user.f
value. If you type window.cljs.user.f
into the console, does it show a value? If you change window.cljs.user.f
to another value, does the call still work?
Ah this is exactly what I wanted to know. I didn't realize there was a window.cljs object for program state. But I can also eval "(dmacs.api/f ..)" and that will call a function f that I defined in the namespace. So eval has access to all program namespaces and functions through this window object?
when you compile without advanced optimizations, the values defined in namespaces are all stored at my.namespace.def_name
,
eg. dmacs.api/f
=> window.dmacs.api.f
Wow thank you so much. When I do get to optimizations, is there a way to preserve these namespaces?
in general, yes, you can use ^:export
, https://clojurescript.org/reference/advanced-compilation#access-from-javascript
eg:
(defn ^:export square [x]
(* x x))
i'm not sure if cljs eval
is supported with advanced optimizations
if you can, I would highly recommend avoiding eval
usage in clojurescript
https://clojurescript.org/reference/bootstrappingf you can't avoid eval, you can check out https://clojurescript.org/reference/bootstrapping and there's a #bootstrapped-cljs channel
using eval in cljs is neat, but its usage has several caveats and it will dramatically increase the size of your shipped js
have you considered https://github.com/borkdude/sci ?