<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged namespaces</title>
<link>https://ask.clojure.org/index.php/tag/namespaces</link>
<description></description>
<item>
<title>File and location data on namespaces</title>
<link>https://ask.clojure.org/index.php/14546/file-and-location-data-on-namespaces</link>
<description>&lt;h3&gt;Preamble&lt;/h3&gt;
&lt;p&gt;Two thoughts:&lt;/p&gt;
&lt;p&gt;Namespaces are not tied to files; they can be created and removed as desired. However, both idiomatically and through &lt;code&gt;clojure.core&lt;/code&gt;, they are &lt;em&gt;typically&lt;/em&gt; tied to files, being specified at the top of a file that shares the same name: &lt;code&gt;src/noahtheduke/splint/rules.clj&lt;/code&gt; holds a single namespace &lt;code&gt;noahtheduke.splint.rules&lt;/code&gt;, which is declared before all other forms. This is enforced in &lt;code&gt;require&lt;/code&gt; and &lt;code&gt;use&lt;/code&gt; calls: &lt;code&gt;(require '[noahtheduke.foo.bar :as fb])&lt;/code&gt; will find a file at &lt;code&gt;src/noahtheduke/bar.clj&lt;/code&gt; and then once the file is loaded will attempt to find the namespace &lt;code&gt;noahtheduke.bar&lt;/code&gt; and raise an error if it does not exist.&lt;/p&gt;
&lt;p&gt;In the compiler, &lt;code&gt;DefExpr&lt;/code&gt; will add to the created var's metadata location data in the form of &lt;code&gt;:line&lt;/code&gt;, &lt;code&gt;:column&lt;/code&gt;, and &lt;code&gt;:file&lt;/code&gt;. The first two come from the seq and latter from &lt;code&gt;*file*&lt;/code&gt;. This allows for introspection and analysis from tooling, and for better error handling in exceptions and messages.&lt;/p&gt;
&lt;h3&gt;Desired functionality&lt;/h3&gt;
&lt;p&gt;I have a collection of namespaces. I want to slurp the corresponding files (if they exist) to analyze and modify with rewrite-clj.&lt;/p&gt;
&lt;h3&gt;Discussion&lt;/h3&gt;
&lt;p&gt;Right now, I use metadata on vars to find relevant objects in code and then find the file that way:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn get-path-from-var [var']
  (let [f (:file (meta var'))
        path (or (some-&amp;gt; f
                         (#(.. (Thread/currentThread)
                               (getContextClassLoader)
                               (getResource %)))
                         (io/file)
                         (str))
                 f)]
    {:path path
     :file (when (and path (.exists (io/file path)))
             (slurp path))}))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, that requires users to use the &lt;code&gt;defcram&lt;/code&gt; macro I've written, which limits the potential for inline-usage of my primary macro &lt;code&gt;compare-output&lt;/code&gt; (such as in a &lt;code&gt;deftest&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;I want to be able to use &lt;code&gt;*ns*&lt;/code&gt; in &lt;code&gt;compare-output&lt;/code&gt; to potentially find where the macro is being called, so I can perform the analysis and desired diffing. This would most easily be done by having location metadata on namespaces.&lt;/p&gt;
&lt;p&gt;But more importantly than just my library, I think having location metadata on namespaces would be a genuine help for many tools (such as eastwood, tools.namespace, CIDER, kibit, splint, etc). &lt;/p&gt;
&lt;h3&gt;Solutions&lt;/h3&gt;
&lt;p&gt;I'm only going to discuss one solution because this is an Ask, not a jira ticket lol.&lt;/p&gt;
&lt;p&gt;I think adding the location metadata in the &lt;code&gt;ns&lt;/code&gt; macro is the best place to put it. The &lt;code&gt;ns&lt;/code&gt; macro is the preferred way to create a new namespace. Creating namespaces with &lt;code&gt;in-ns&lt;/code&gt; or &lt;code&gt;create-ns&lt;/code&gt; requires additional steps to make the namespace usable, and neither is a macro to hold &lt;code&gt;&amp;amp;form&lt;/code&gt; metadata, increasing the work of adding location metadata.&lt;/p&gt;
</description>
<category>Metadata</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14546/file-and-location-data-on-namespaces</guid>
<pubDate>Fri, 16 May 2025 14:46:20 +0000</pubDate>
</item>
<item>
<title>Why does running an uberjar reload namespaces?</title>
<link>https://ask.clojure.org/index.php/13839/why-does-running-an-uberjar-reload-namespaces</link>
<description>&lt;p&gt;Consider this basic project:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns whatever.core
  (:gen-class))

(def some-var (doto (System/getenv &quot;FOO&quot;) println))

(defn -main [&amp;amp; args] (println some-var))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If I make an uberjar out of this, I can see that during compilation, &lt;code&gt;nil&lt;/code&gt; is printed (as expected). However, when I run it with environment variables, I expect nil, but I can see that the correct value is printed and &lt;code&gt;some-var&lt;/code&gt; is actually re-bound! Even though that sounds kind of 'convenient' (for this use-case), I fail to understand why it happens...Since I AOT'ed the namespace, I would expect to have to defer getting the env-var until runtime (e.g. via &lt;code&gt;delay&lt;/code&gt; or something), for this work. Also what are the implications of this double loading? What if I had some very expensive computation in a def, and really wanted to pre-compute it once (and only once)? From what I can tell all def expressions (reachable from &lt;code&gt;-main&lt;/code&gt;) are loaded anew when the program starts, regardless of AOT... Help me understand this please, as it's driving me crazy!&lt;/p&gt;
&lt;p&gt;Many thanks in advance...&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13839/why-does-running-an-uberjar-reload-namespaces</guid>
<pubDate>Thu, 25 Apr 2024 18:43:53 +0000</pubDate>
</item>
<item>
<title>&quot;namespace X is required but never used&quot; but uberjar compiles and runs it anyway?</title>
<link>https://ask.clojure.org/index.php/12506/namespace-required-never-used-uberjar-compiles-runs-anyway</link>
<description>&lt;p&gt;So... to be quick, this is my core.clj file:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns ffmpeg-in-a-jar.core
  (:gen-class)
  (:require [ffmpeg-in-a-jar.encoder :as encoder]))

(defn -main
  &quot;ffmpeg autoencoder made with Clojure&quot;
  [&amp;amp; args]
  (println &quot;Files will begin encoding...&quot; ))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;on encoder.clj file i've defined an user-defined loop counter:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn loop-cnt []
  (println &quot;How many files to encode?&quot;)
  (def loopnum (read-string (read-line)))
  (println &quot;\n&quot;&quot;Files ready to encode&quot;))

(loop-cnt)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;it is right after used in a loop to again request the user for more input:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn queue []
  (loop [loopnum loopnum
         file-queue []]
   (if (not= loopnum 0)
     (do
       (def file-queue [])
       (println &quot;\n&quot;&quot;Write filenames one by one&quot;)
       (def add-queue (read-line))
       (recur (dec loopnum) (conj file-queue add-queue)))
     file-queue)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;im calling these with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(loop-cnt)
(def queue-result (queue))
(println &quot;\n&quot;&quot;Added to the Encoding Queue:&quot; &quot;\n&quot; queue-result &quot;\n&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The real doubt im having is, how in the world is encoder even working, i use require to make it available but i've yet to call it inside -main with i guess should be:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(encoder/loop-cnt)
(println &quot;\n&quot; &quot;Added to the Encoding Queue:&quot; &quot;\n&quot; encoder/queue-result &quot;\n&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;is the ns macro doing something im unaware of?&lt;/p&gt;
</description>
<category>Namespaces and vars</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12506/namespace-required-never-used-uberjar-compiles-runs-anyway</guid>
<pubDate>Sun, 25 Dec 2022 01:35:35 +0000</pubDate>
</item>
<item>
<title>Help me understand the var basics in Clojure.</title>
<link>https://ask.clojure.org/index.php/11234/help-me-understand-the-var-basics-in-clojure</link>
<description>&lt;p&gt;We know that when we send a symbol to REPL, Clojure will resolve the symbol to var, and get the value which is currently the var's bindings. And we also know that var object is stable when it is interned by namespace, so we can dynamically modify the runtime behavior of a program(by re-def-ing the var's bindings).&lt;br&gt;
But here comes a strange things that I don't understand.&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://drive.google.com/file/d/1HaqwYCRFiKs9ZvJrK33RsIZgNGZSxvap/view&quot;&gt;Screenshot of the REPL process&lt;/a&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user=&amp;gt; (ns ns-a)
ns-a=&amp;gt; (def a 42)
ns-a=&amp;gt; a
;;42
ns-a=&amp;gt; (ns ns-b)
ns-b=&amp;gt; (refer 'ns-a)
ns-b=&amp;gt; a
;;42
ns-b=&amp;gt; (var a)
#'ns-a/a
ns-b=&amp;gt; (in-ns 'ns-a)
ns-a=&amp;gt; a
;;42
ns-a=&amp;gt; (def a 99)
ns-a=&amp;gt; a
;;99
ns-a=&amp;gt; (in-ns 'ns-b)
ns-b=&amp;gt; a
;;99
;;nothing special so far
ns-b=&amp;gt; (in-ns 'ns-a)
ns-a=&amp;gt; (ns-publics *ns*)
;;{a #'ns-a/a}
;;see, it's interned
ns-a=&amp;gt; (ns-unmap *ns* 'a)
;;lets unmap the (var a) from ns-a
ns-a=&amp;gt; (ns-publics *ns*)
;;{}
ns-a=&amp;gt; a
;;Syntax error compiling at (REPL:0:0).
;;Unable to resolve symbol: a in this context
ns-a=&amp;gt; (var a)
;;Syntax error compiling var at (REPL:1:1).
;;Unable to resolve var: a in this context
;;(var a) is nowhere.
ns-a=&amp;gt; (in-ns 'ns-b)
ns-b=&amp;gt; a
;;99
;;the referred symbol a still evaluates to 99.
ns-b=&amp;gt; (var a)
;;#'ns-a/a
;;yeah, (var a) is still somewhere.
ns-b=&amp;gt; (deref (var a))
;;#object[clojure.lang.Var$Unbound 0x4a68135e &quot;Unbound: #'ns-a/a&quot;]
;;What? (var a) is being unbound. Is this a new (var a)?
ns-b=&amp;gt; a
;;99
;;it is not resolved from (var a)?
ns-b=&amp;gt; (in-ns 'ns-a)
ns-a=&amp;gt; a
;;#object[clojure.lang.Var$Unbound 0x4a68135e &quot;Unbound: #'ns-a/a&quot;]
;;What? it comes back?
ns-a=&amp;gt; (def a 53)
;;Lets redefine (var a) to bind 53
ns-a=&amp;gt; a
;;53
ns-a=&amp;gt; (in-ns 'ns-b)
;;Lets switch back and check.
ns-b=&amp;gt; a
;;99
;;not changed
ns-b=&amp;gt; (deref (var a))
;;53
;;so I'm sure 99 it is not resolved from (var a).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;The first question&lt;/strong&gt;: where is the value 99 resides? It used to being resolved from #'a, but it seems being like a lexical bindings after #'a being unmapped. Is it?&lt;br&gt;
&lt;strong&gt;The second question&lt;/strong&gt;: why the #'a is interned back with unbound state after execute (var a) in another namespace(which has a refering to #'a)? Seems it create a new var to fullfill the state.&lt;/p&gt;
</description>
<category>Namespaces and vars</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11234/help-me-understand-the-var-basics-in-clojure</guid>
<pubDate>Mon, 01 Nov 2021 06:25:40 +0000</pubDate>
</item>
<item>
<title>How do you access namespace values in a macro?</title>
<link>https://ask.clojure.org/index.php/10965/how-do-you-access-namespace-values-in-a-macro</link>
<description>&lt;p&gt;I'm working in clojurescript, where &lt;code&gt;ns-interns&lt;/code&gt; is a macro.&lt;/p&gt;
&lt;p&gt;I'm trying to write a macro that gets information about the internal bindings in a namespace, but I'm not able to manipulate the bindings at compile time.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro my-macro [namespace]
   (let [mappings (ns-interns namespace)]
           ; do stuff
       ))

(my-macro 'mylib.hi)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The trouble is that the above fails because the &lt;code&gt;ns-interns&lt;/code&gt; macro think it's processing the symbol &lt;code&gt;namespace&lt;/code&gt;, and not the namespace I pass into &lt;code&gt;my-macro&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I can try&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[mappings `(ns-interns ~namespace)]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;, but then I can't expand the macro at compile-time into a map that I can use to generate code -- the only thing I can do is &quot;print&quot; it in the generated code. I tried using &lt;code&gt;macroexpand&lt;/code&gt; and &lt;code&gt;macroexpand-all&lt;/code&gt;, but they did not work for me -- I couldn't use them to get the results of &lt;code&gt;ns-interns&lt;/code&gt; for use in my macro.&lt;/p&gt;
&lt;p&gt;However, macros can be expanded, since the below will work if I hard-code the namespace symbol.  The trouble comes because I want to pass the outer macro's argument into an inner macro. What's the right way to solve approach this, where I want to get and manipulate the vars of a namespace at compile-time in Clojurescript?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro my-macro [namespace]
   (let [mappings (ns-interns 'mylib.hi)]
     ; do stuff
   )
)
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10965/how-do-you-access-namespace-values-in-a-macro</guid>
<pubDate>Mon, 23 Aug 2021 15:58:29 +0000</pubDate>
</item>
<item>
<title>Evaluating an ns form in a ClojureScript REPL throws RangeError</title>
<link>https://ask.clojure.org/index.php/10602/evaluating-an-ns-form-clojurescript-repl-throws-rangeerror</link>
<description>&lt;p&gt;What it says on the tin.&lt;/p&gt;
&lt;p&gt;Minimal repro here: &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/eerohele/cljs-repl-blow-stack&quot;&gt;https://github.com/eerohele/cljs-repl-blow-stack&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I'm trying to make the simplest possible ClojureScript REPL. Am I doing something wrong, or is this a ClojureScript bug?&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10602/evaluating-an-ns-form-clojurescript-repl-throws-rangeerror</guid>
<pubDate>Thu, 13 May 2021 09:45:36 +0000</pubDate>
</item>
<item>
<title>Listing all keywords in a namespace</title>
<link>https://ask.clojure.org/index.php/10044/listing-all-keywords-in-a-namespace</link>
<description>&lt;p&gt;In clojure, symbols can optionally belong to a namespace:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;:FileType/TXT
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is it possible to list all keywords that are defined for a given keyword namespace?&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10044/listing-all-keywords-in-a-namespace</guid>
<pubDate>Wed, 13 Jan 2021 17:04:23 +0000</pubDate>
</item>
<item>
<title>Bind *ns* and require before evaluating namespace metadata</title>
<link>https://ask.clojure.org/index.php/10005/bind-ns-and-require-before-evaluating-namespace-metadata</link>
<description>&lt;p&gt;Before evaluating its namespace metadata, I wish ns macro would:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Have &lt;code&gt;*ns*&lt;/code&gt; be bound already.&lt;/li&gt;
&lt;li&gt;:require, to be able to use :as namespace names in metadata expression.&lt;br&gt;
Example:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Current (uses fully qualified names):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    (ns my.app.foo.controller
      {:tape.mvc/interceptors [(re-frame.core/path
                                [:my.app.foo.controller/controller])]}
      (:require [re-frame.core :as rf]
                [tape.mvc :as mvc]))

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After change (uses &quot;shortcut&quot; names):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    (ns my.app.foo.controller
      {::mvc/interceptors [(rf/path [::controller])]}
      (:require [re-frame.core :as rf]
                [tape.mvc :as mvc]))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Would this change be worthwile/acceptable/feasible?&lt;/p&gt;
</description>
<category>Metadata</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10005/bind-ns-and-require-before-evaluating-namespace-metadata</guid>
<pubDate>Tue, 05 Jan 2021 20:15:04 +0000</pubDate>
</item>
<item>
<title>I think there's a typo in spec2 that breaks if you don't alias clojure.alpha.spec -&gt; s</title>
<link>https://ask.clojure.org/index.php/9811/think-theres-typo-spec2-that-breaks-alias-clojure-alpha-spec</link>
<description>&lt;p&gt;In a project, I required + aliased &lt;code&gt;clojure.alpha.spec&lt;/code&gt; like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns example.core
    (:require [clojure.alpha.spec :as spec]))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then I tried to define a simple spec like&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(spec/def ::example-spec #(not (neg? %)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Compiling gave the error that namespace &lt;code&gt;s&lt;/code&gt; was undefined. &lt;/p&gt;
&lt;p&gt;I believe this came down to a typo in &lt;code&gt;clojure.alpha.spec/def&lt;/code&gt;. In file &lt;code&gt;spec-alpha2/src/main/clojure/clojure/alpha/spec.clj&lt;/code&gt;,  line 456, there is a backquoted &lt;code&gt;s/spec&lt;/code&gt; but the alias at the top of the file is to &lt;code&gt;sa&lt;/code&gt;. Here's line 456:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    (#{'fn 'fn* `c/fn} op) `(s/spec ~explicated-form)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; After changing the line to &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(#{'fn 'fn* `c/fn} op) `(sa/spec ~explicated-form)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;my &lt;code&gt;spec/def&lt;/code&gt; works successfully. &lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9811/think-theres-typo-spec2-that-breaks-alias-clojure-alpha-spec</guid>
<pubDate>Mon, 16 Nov 2020 06:12:55 +0000</pubDate>
</item>
<item>
<title>Multiple `:require` in `ns`</title>
<link>https://ask.clojure.org/index.php/9179/multiple-require-in-ns</link>
<description>&lt;p&gt;CLJ accepts multiple &lt;code&gt;:require&lt;/code&gt; declarations in &lt;code&gt;ns&lt;/code&gt; form.&lt;br&gt;
CLJS allows only one declaration.&lt;/p&gt;
&lt;p&gt;Which behavior is correct?&lt;br&gt;
Can we request for support of multiple “requires” in CLJS?&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9179/multiple-require-in-ns</guid>
<pubDate>Sun, 22 Mar 2020 17:47:51 +0000</pubDate>
</item>
<item>
<title>Namespace.java: addAlias exception could be more informative</title>
<link>https://ask.clojure.org/index.php/8705/namespace-java-addalias-exception-could-more-informative</link>
<description>&lt;p&gt;In &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/653b8465845a78ef7543e0a250078eea2d56b659/src/jvm/clojure/lang/Namespace.java#L224-L225&quot;&gt;https://github.com/clojure/clojure/blob/653b8465845a78ef7543e0a250078eea2d56b659/src/jvm/clojure/lang/Namespace.java#L224-L225&lt;/a&gt;, the &lt;code&gt;ns&lt;/code&gt; argument is not included in the exception message.&lt;/p&gt;
&lt;p&gt;This hinders debuggabiliity, especially when the &lt;code&gt;.addAlias&lt;/code&gt; call didn't originate from intentful aliasing, but rather, as a result of re-evaluating a namespace (which is the AST-building strategy that tools such as &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/jonase/eastwood&quot;&gt;https://github.com/jonase/eastwood&lt;/a&gt; use).&lt;/p&gt;
&lt;p&gt;Have you considered improving the message?&lt;/p&gt;
</description>
<category>Errors</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8705/namespace-java-addalias-exception-could-more-informative</guid>
<pubDate>Thu, 10 Oct 2019 15:55:01 +0000</pubDate>
</item>
</channel>
</rss>