Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Namespaces Clojure:引用名称空间的名称_Namespaces_Clojure - Fatal编程技术网

Namespaces Clojure:引用名称空间的名称

Namespaces Clojure:引用名称空间的名称,namespaces,clojure,Namespaces,Clojure,在Clojure中,如何获取命名变量和函数的名称空间的名称?例如,改进以下方面: (ns my-ns) (def namespace-name "my-ns") 上面的问题是,如果我想更改我的ns的名称,我还必须更改名称空间名称的定义当前名称空间存储在 *ns* 因为函数是在运行时计算的,所以调用函数时,会得到*ns*的值 所以如果你想保存它的副本 对亚瑟回答的简单修改很有效 (def namespace-name (ns-name *ns*)) 但是我想提醒Clojure初学者 (d

在Clojure中,如何获取命名变量和函数的名称空间的名称?例如,改进以下方面:

(ns my-ns)

(def namespace-name "my-ns")

上面的问题是,如果我想更改我的ns的名称,我还必须更改名称空间名称的定义当前名称空间存储在

*ns* 
因为函数是在运行时计算的,所以调用函数时,会得到*ns*的值


所以如果你想保存它的副本

对亚瑟回答的简单修改很有效

(def namespace-name (ns-name *ns*))
但是我想提醒Clojure初学者

(defn namespace-name [] (ns-name *ns*))

就本问题而言,将不起作用,因为*ns*是动态绑定的。

要创建和存储命名空间,可以执行以下操作:

user=> (def working-namespace (create-ns 'my-namespace))
#'user/working-namespace

user=> working-namespace
#<Namespace my-namespace>

user=> (class working-namespace)
clojure.lang.Namespace

在Michael Kohl的帮助下,我发现了如何切换到变量()中的名称空间

那么,我们开始吧:

user=> (def working-namespace (create-ns 'my-namespace))
#'user/working-namespace
user=> (in-ns (symbol (str working-namespace) ))
#<Namespace my-namespace>
my-namespace=>
;; notice how it switched to "my-namespace"

;; now if i were to put some other namespace in that variable...
my-namespace=> (ns user)
nil
user=> (def working-namespace (create-ns 'other-namespace))
#'user/working-namespace

;; and switch again, i would get the new namespace
user=> (in-ns (symbol (str working-namespace) ))
#<Namespace other-namespace>
other-namespace=> ; tadaa!

你的意思是(def名称空间名称(ns name*ns*)?我键入ns,它显示为斜体?如何编写它,使其按字面意思显示?
*ns*
可以用反勾来编写,我想
(clojure.test/run-tests*ns*)
但是要遵循以下原则(defmacro defns name[name]`(def~name(ns name)(ns name))然后调用(defns name namespace name)可以。在上面尼古拉斯的评论中,标记一下
ns
周围的星星:应该是:
(defmacro defns name[name]`(def~name(ns name*ns*))
user=> (def working-namespace (create-ns 'my-namespace))
#'user/working-namespace
user=> (in-ns (symbol (str working-namespace) ))
#<Namespace my-namespace>
my-namespace=>
;; notice how it switched to "my-namespace"

;; now if i were to put some other namespace in that variable...
my-namespace=> (ns user)
nil
user=> (def working-namespace (create-ns 'other-namespace))
#'user/working-namespace

;; and switch again, i would get the new namespace
user=> (in-ns (symbol (str working-namespace) ))
#<Namespace other-namespace>
other-namespace=> ; tadaa!
user=> (intern working-namespace 'some-var "my value")
#'other-namespace/some-var

user=> (var-get (intern working-namespace 'some-var))
"my value"