我该如何获取命名空间元数据?在repl中我这样做:
=> (in-ns my.test)
#<Namespace my.test>
=> (meta *ns*)
{:a "hello"}
=> ; Return back to repl namespace:
(in-ns 'user)
; Or use 'with-ns
我的命名空间定义如下:
(ns ^{:a "hello"} my.test
...)
任何(meta#'my.test)
变体均失败,尽管它对于def
-ined实体有效。我可以理解(ns ...)
不定义变量,* ns *
是一个特殊变量,我无法在外部引用,但我怀疑究竟发生了什么。
(meta (the-ns 'foo.core))
或(meta *ns*)
可以用来检索提供的元数据。 - Jared314#'my.test
的部分。谢谢! @demi#'
用于引用 Vars(它是var
特殊形式的简写);要根据名称获取命名空间,可以使用the-ns
或find-ns
。 - Michał Marczyk(source the-ns)
:命名空间是一个独立的实体,不是变量,所以为什么#'my.test
不起作用。但为什么(meta 'my.test)
失败了?从源代码来看 -the-ns
只为x
分配类型,因此在meta
中采取适当的分支。如果没有这个,它只是一个没有元数据的符号。 - demi(meta ^clojure.lang.Namespace 'my.test)
会失败呢?是:static
属性的魔力吗? - demithe-ns
检查其参数是否为命名空间对象。如果是,则返回该对象;如果不是,则假定它是一个符号,并使用find-ns
查找该名称的命名空间。在后一种情况下,如果find-ns
无法找到适当的命名空间,则the-ns
抛出异常。(2)(meta 'my.test)
询问与符号'my.test
(由读取器返回)附加的元数据。这里没有附加元数据,因此正确的返回值是nil
。(3) 对Namespace
进行'my.test
的提示毫无意义——它实际上是一个符号。(4)#'my.test
是(var my.test)
的简写形式。 - Michał Marczyk