3

How can I get namespace metadata? In repl I do:

=> (in-ns my.test)
#<Namespace my.test>
=> (meta *ns*)
{:a "hello"}
=> ; Return back to repl namespace:
   (in-ns 'user)
   ; Or use 'with-ns

My namespace definition is:

(ns ^{:a "hello"} my.test
  ...)

Any of (meta #'my.test) variants fail, although it works good for def-ined entities. I can understand that (ns...) doesn't define a variable and *ns* is a special var that I cannot reference outside, but I'm in doubt what really happens.

4

1 回答 1

3

AFAICT 存在一个长期存在的问题,即附加到传递给ns表单的符号名称的元数据未正确附加到命名空间对象。

作为替代方案,您可以将其他参数传递给ns

(ns foo.core
  "This is foo.core's excellent docstring."
  {:interesting "piece of metadata"}
  (:require [bar.core :as bar]))

这种风格按预期工作。我也碰巧发现它在美学上更令人愉悦。

注意。其他引入命名对象的 Clojure 宏也倾向于以这种方式接受额外的文档字符串/元数据参数:

(defn foo
  "A particularly lucid docstring for foo."
  {:trivia "The first function named foo was written in ..."}
  []
  ; ...
  )
于 2013-09-17T23:54:22.110 回答