How can I get the Clojurescript namespace I got from from clojurescript?

How can I get the Clojurescript namespace I got from from clojurescript? I want to do this by providing some debugging information (it should only work in dev mode)

+6
source share
4 answers

Namespaces are not the first class in ClojureScript because they are in Clojure. There is absolutely no way to get a namespace at runtime. This information can be obtained in macro expansion mode if you are not afraid to access some of the internal components of the ClojureScript compiler. There probably should be an API for this, but we're not there yet.

+7
source

You can get the name of the current namespace using this trick, which uses :: creating a character with names for you in the current namespace:

 (namespace ::x) 

You probably don't want to use this value for anything, because if the code is compiled, the internal representation will change. If you want to live in danger, then in the browser you can access the js object, which contains a namespace like this:

 (reduce (fn [ns n] (aget ns n)) js/window (clojure.string/split (namespace ::x) #"\.")) 
+6
source

During macro decomposition, you can access &env and get namespace information using the :ns key as follows:

 (:ns &env) (:name (:ns &env)) 

This only works in macro extension / compilation mode, not at run time.

+3
source

You can try this

 (apply str (drop-last 2 (str `_))) 
+1
source

Source: https://habr.com/ru/post/945451/


All Articles