将文档字符串放在数据变量上,这被认为是惯用的吗?

发布于 2024-09-28 05:08:01 字数 270 浏览 0 评论 0原文

所以我定义了一些变量来在我的 clojure 代码中保存状态数据。我刚刚发现我可以向这些变量添加文档字符串,例如:

(def ^{:doc "Documentation for *my-var*"}
        *my-var*)

这让我可以在 REPL 上调用 (doc *my-var*) 。这似乎是一件有效且有用的事情,但在我读过的(有限的)代码中,这似乎并不常见。

这被认为是惯用的 clojure 吗?

So I have defined some vars to hold state data in my clojure code. I have just discovered I can add a doc-string to those vars e.g.:

(def ^{:doc "Documentation for *my-var*"}
        *my-var*)

That lets me call (doc *my-var*) at the REPL. This seems like a valid and useful thing to do but it doesn't seem like common practice in the (limited) code I have read.

Is this considered idiomatic clojure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

臻嫒无言 2024-10-05 05:08:01

从 clojure 1.3 开始,def 允许使用可选的文档字符串。

(def *my-var*
  "My var does cool things (it really doesn't)."
  nil)

Since clojure 1.3, def has allowed an optional docstring.

(def *my-var*
  "My var does cool things (it really doesn't)."
  nil)
娇俏 2024-10-05 05:08:01

也用于 Clojure 命名空间(如 clojure.pprint):

(def
 ^{:doc "The base to use for printing integers and rationals."
   :added "1.2"}
 *print-base* 10)

您可能不想使用 clojure.contrib.def 中的便捷宏:

(defvar *my-var*
  nil
  "Documentation for *my-var*")

Also used in Clojure namespaces (like clojure.pprint):

(def
 ^{:doc "The base to use for printing integers and rationals."
   :added "1.2"}
 *print-base* 10)

You may wan't to use a convenience macro from clojure.contrib.def:

(defvar *my-var*
  nil
  "Documentation for *my-var*")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文