各种 Clojure 风格指南建议避免行长超过 80 个字符。我想知道是否有一种惯用的方法来避免长 String
文字。
虽然现在宽屏幕很常见,但我仍然同意应该避免排长队。
这里有一些例子(我很想遵循第一个):
;; break the String literal with `str`
(println (str
"The quick brown fox "
"jumps over the lazy dog"))
;; break the String literal with `join`
(println (join " " [
"The quick brown fox"
"jumps over the lazy dog"]))
我知道 Clojure 支持多行 String
文字,但使用这种方法会对解释的换行符产生不良影响,例如使用repl
:
user=> (println "The quick brown fox
#_=> jumps over the lazy dog")
The quick brown fox
jumps over the lazy dog
最佳答案
您可能应该将字符串存储在外部文本文件中,并从代码中读取该文件。如果您仍然觉得需要在代码中存储字符串,请继续使用 str
。
编辑:
根据要求,我将演示如何在编译时读取长字符串。
(defmacro compile-time-slurp [file]
(slurp file))
像这样使用它:
(def long-string (compile-time-slurp "longString.txt"))
您可以发明类似的宏来处理 Java 属性文件、XML/JSON 配置、SQL 查询、HTML 或您需要的任何其他内容。
关于clojure - 有没有一种惯用的方法来避免长 Clojure 字符串文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21098784/