将文档字符串放在数据变量上,这被认为是惯用的吗?
所以我定义了一些变量来在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 clojure 1.3 开始,
def
允许使用可选的文档字符串。Since clojure 1.3,
def
has allowed an optional docstring.也用于 Clojure 命名空间(如 clojure.pprint):
您可能不想使用 clojure.contrib.def 中的便捷宏:
Also used in Clojure namespaces (like clojure.pprint):
You may wan't to use a convenience macro from clojure.contrib.def: