【问题标题】:Using a function to generate a docstring in Clojure在 Clojure 中使用函数生成文档字符串
【发布时间】:2014-05-01 05:11:07
【问题描述】:

我希望能够以编程方式为我在 clojure 中的一个函数定义一个文档字符串。

例如,我希望能够做这样的事情:

(defn my-function
    (str "Here are some numbers " (range 10))
    []
    (println "This function does nothing right now."))

但是,当我尝试这样做时,我得到“参数 decleration str 应该是一个向量”。这在clojure中是不可能的还是有一些偷偷摸摸的方法来做到这一点?以编程方式生成部分文档字符串对我很有用。

【问题讨论】:

    标签: clojure


    【解决方案1】:

    是的,这绝对是可能的。您在这里遇到的问题是 defn(或者更确切地说,它扩展为的 def 特殊形式)将文档字符串附加到符号上,如果第二位的参数是字符串。

    您可以通过自己设置:doc 元数据来规避此问题。

    (defn ^{:doc (apply str "Here are some numbers " (range 10))} my-function
       []
       (println "This function does nothing right now."))
    

    或者可能通过编写自己的宏 - 但我认为以上是最直接的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      • 2013-03-19
      • 1970-01-01
      相关资源
      最近更新 更多