Fork me on GitHub
#clojurescript
<
2019-05-09
>
ag23:05:57

is it possible to introduce a def programmatically in Clojurescript without using a macro? I can’t use clojure.core/intern, using eval also didn’t work as I expected

lilactown23:05:02

there’s no such thing as vars in CLJS

lilactown23:05:19

what are you trying to do exactly?

ag23:05:38

well… I need to introduce a bunch of defs in a namespace

lilactown23:05:01

but what for? 😄

dpsutton23:05:42

could you have a shadow-environment? (def runtime-environment (atom {})) and instead of defing things are runtime rather (swap runtime-environment assoc (name x) x)) or something?

lilactown23:05:50

if it’s just to clean up some boilerplate, a macro might suffice. if it’s to do some other kind of runtime malarkey, another solution might work

ag23:05:22

I guess I’ll have to do it with a macro

lilactown23:05:47

so mysterious @ag 😛

ag23:05:02

I have this js/BigObject with bunch of keys, we have a namespace that basically manually does something like (def foo (adapt-react-class (gobj/get js/BigObject “foo”)). I need to find a way to do it more cleanly

lilactown23:05:49

yeah, a macro will give you cleaner access via namespace, but you’ll have to enumerate the keys

lilactown23:05:09

there’s not a way to do this at runtime AFAIK that wouldn’t break advanced compilation

ag23:05:46

js/Object.keys ?

lilactown23:05:13

the issue is defining it in the namespace

lilactown23:05:32

at macro time, you can’t call Object.keys because you’re running Clojure

lilactown23:05:12

at runtime, you can’t define a variable in a namespace because it might (will) be renamed when advanced optimizations are done

ag23:05:46

isn’t there a meta attribute you can use that it doesn’t munge them or something?

lilactown23:05:14

only at compile time

lilactown23:05:41

all def calls get compiled away

lilactown23:05:32

they turn into something like: my_ns.components.some_component = ... and then closure compiler takes that and turns it into: a1.cn.o3 = ...

lilactown23:05:54

so you have to note at compile time which ones to not optimize away

ag23:05:15

orhgh… sometimes I hate my life