【问题标题】:What is the difference between ; and ;; in Clojure code comments?和有什么区别?和 ;;在 Clojure 代码注释中?
【发布时间】:2011-07-02 08:03:02
【问题描述】:

在 Clojure 中开始评论时,;;; 有什么区别?我看到我的text editor 给它们着色不同,所以我假设在理论上存在一些差异。

我还看到Marginalia 对待它们的方式不同:

; Stripped entirely
;; Appears in text section of marginalia
(defn foobar []
   ; Appears in code section of marginalia output
   ;; Again, appears in code section of marginalia output
   6)

【问题讨论】:

    标签: clojure comments marginalia


    【解决方案1】:

    就解释器而言,没有区别。将;;;;;;;;;; 视为不同的标题级别。

    这是我个人的使用约定:

    ;;;; Top-of-file level comments, such as a description of the whole file/module/namespace
    
    ;;; Documentation for major code sections (i.e. groups of functions) within the file.
    
    ;; Documentation for single functions that extends beyond the doc string (e.g. an explanation of the algorithm within the function)
    
    ; In-line comments possibly on a single line, and possibly tailing a line of code
    

    【讨论】:

    • 鉴于赞成票的数量,我想我必须将其标记为正确。外部参考会很好,但我想这是一种不成文的文化。 :)
    • 作为参考,Good Lisp Programming Style 的作者 Peter Norvig 将这种约定称为“接近标准”。 stackoverflow.com/a/4531930/603891
    • 那为什么在文档中没有提到单个;clojuredocs.org/clojure.core/comment
    • @matanster 该链接是关于(评论)而不是;。但是最大例子中的FTR第二段确实讨论了一个;。对于 ; 上的规范文档阅读器宏检查clojure.org/reference/reader#macrochars
    【解决方案2】:

    查看elisp中;;;的含义的official description:由于Clojure压头基本相同,因此将它们类似地对待。基本上,如果您正在“在页边距”中编写一个跨越多行但应被视为单个实体的长句子/描述,请使用;。他们的例子是:

    (setq base-version-list                 ; there was a base
          (assoc (substring fn 0 start-vn)  ; version to which
                 file-version-assoc-list))  ; this looks like
                                            ; a subversion
    

    压头将确保它们彼此相邻排列。相反,如果您想使几个不相关的单行 cmets 彼此相邻,请使用 ;;

    (let [x 99 ;; as per ticket #425
          y "test"] ;; remember to test this
      (str x y)) ;; TODO actually write this function
    

    【讨论】:

      【解决方案3】:

      Emacs ;用于行尾 cmets,如果这不是您的意图,则会以令人惊讶的方式缩进。 ;;不,所以我通常使用;;。

      Clojure 不在乎 - 从 ; 中忽略任何行到停产。

      我相信 CL 有一个使用越来越多的 ; 的传统。表示更重要的 cmets/section。

      【讨论】:

        【解决方案4】:

        对语言没有意义。 ;comment 的阅读器宏 也许其他工具会解析它们,但“在 clojure 内”它们是相同的。

        【讨论】:

          【解决方案5】:

          从 Clojure 角度来看没有区别。我发现;;; 突出一点,但这只是我的意见。

          另一方面,Marginalia 对它们的处理方式不同,因为有时注释应该保留在代码部分(例如许可证)中,而这些注释标记为 ;。这是一个武断的决定,将来可能会改变。

          【讨论】:

          • fogus,请注意 alex miller 对 emacs 的评论。它缩进单个; cmets 但不是 ;;要么 ;;;如果你自己不使用 emacs,你应该知道它的自动缩进方案是做什么的,因为你的很多目标用户都会。
          【解决方案6】:

          在包括 clojure-mode 在内的 emacs lisp 模式中,;; 被格式化为位于行首的约定,并根据上下文与任何其他行一样缩进。 ; 预计将在行尾使用,因此如果您在行首放一个单分号注释并期望它在当前上下文的缩进处使用制表符,emacs 将不会执行您想要的操作.

          例子:

          (let [foo 1]
            ;; a comment
            foo) ; a comment
          

          【讨论】:

            【解决方案7】:

            我不确定(没有使用过 Clojure,以前从未听说过),但this thread可能有帮助。

            【讨论】:

            • 这些样式看起来与可变宽度字体完全不兼容。我希望 Clojure 社区没有采用 cmets 的列对齐方式。
            猜你喜欢
            • 2018-01-14
            • 2022-01-21
            • 2012-11-10
            • 2019-01-25
            • 1970-01-01
            • 2013-07-07
            • 2016-09-16
            • 2014-06-27
            • 2018-11-23
            相关资源
            最近更新 更多