On Tuesday, June 20, 2017 at 7:43:53 PM UTC-5, Didier wrote: > > *ns* is a dynamic var, so it points to the current namespace when your >> function is running. Most code doesn't switch into a target ns in order to >> execute functions from it. >> > > I understand that, and I understand most code does not switch into the > target ns. But this is not most code, this is the code generated by the > gen-class directive to bootstrap your -main method from java. Both lein and > clojure.main bootstrap your main method from inside "user", but gen-class > does so from inside "clojure.core". > > So I want to know: > > 1) Why does the generated java class from gen-class has "clojure.core" as > its current namespace? >
gen-class causes a namespace to be AOT compiled to a class. Leiningen runs the AOT compilation by invoking the compiler. The initial value of *ns* is clojure.core (see RT.java <https://github.com/clojure/clojure/blob/d7e92e5d71ca2cf4503165e551859207ba709ddf/src/jvm/clojure/lang/RT.java#L221-L222>). During compilation, nothing ever changes this, so that's what gets compiled into the Java class on disk. When you invoke it, this value has been compiled into the class already. When you invoke -main directly, you are running in the repl. The repl sets the value <https://github.com/clojure/clojure/blob/d7e92e5d71ca2cf4503165e551859207ba709ddf/src/clj/clojure/main.clj#L307> of *ns* to "user" when it starts, so that's what you see. > 2) Why doesn't the (ns) function inside my namespace change the current > namespace? > It does, but only when you run it. The AOT compiled class also creates an init class that will cause the same effects as loading a non-compiled clj file. That file has already expanded `ns` but it will include a call to `in-ns` that will change the current namespace. However, the compiled class for the -main function already has the value at the time of compilation, so that doesn't matter. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.