I'm experimenting with ns
in Clojure, here's what I try:
user=> (in-ns 'some-ns)
#<Namespace some-ns>
some-ns=> (def aa 100)
#'some-ns/aa
some-ns=> (in-ns 'user)
#<Namespace user>
user=> (= some-ns/aa 100)
true
user=> (= user/aa 100)
CompilerException java.lang.RuntimeException: No such var: user/aa, compiling:(NO_SOURCE_PATH:5:1) ;this works as expected
user=> (defn function [] (in-ns 'some-other-ns) (def cc 100) (in-ns 'user))
#'user/function
user=> (function)
#<Namespace user>
user=> (= some-other-ns/cc 100)
CompilerException java.lang.RuntimeException: No such var: some-other-ns/cc, compiling:(NO_SOURCE_PATH:8:1)
user=> (= user/cc 100)
true
I'm confused, why it doesn't work in function? Also, I tried following:
user=> (binding [*ns* (create-ns 'some-a-ns)] (def dd 100))
#'user/dd
user=> (= some-a-ns/dd 100)
CompilerException java.lang.RuntimeException: No such var: some-a-ns/dd, compiling:(NO_SOURCE_PATH:11:1)
user=> (= user/dd 100)
true
according to clojure doc
Creates and interns or locates a global var with the name of symbol and a namespace of the value of the current namespace (*ns*).
what I'm missing?
PS. I know I can use (intern 'some-ns 'a 100)
, but what I really want is a generic function/macro to do like
(with-ns 'some-ns (def a 100))
(= some/a 100)
(do (in-ns 'some-a-ns) (def dd 100))
- definesdd
assome-a-ns/dd
– Petr Gladkikh