Clojure中(defn {:style/indent 2})语法含义及该配置作用咨询
{:style/indent 2} in Clojure defn Definitions Great question! This is all about editor/IDE indentation metadata—a handy Clojure feature that keeps your code clean and consistent without affecting runtime behavior. Let’s break it down step by step:
What’s the structure of this defn variant?
Normally, a standard defn looks like this:
(defn function-name [parameters] (println "Function Body"))
The {:style/indent 2} map you’re seeing is metadata attached to the function. Clojure lets you insert a metadata map between the function name and parameter vector to add extra, non-functional information to the function object.
What does {:style/indent 2} do?
This specific metadata is for code editors and IDEs that support Clojure (like Cursive, VS Code with Calva, or Emacs clojure-mode). It tells the editor how to properly indent calls to your custom function.
The number 2 defines the indentation rule:
- When you split a call to this function across multiple lines, lines after the first should be indented by 2 indentation units (usually 2 spaces, depending on your editor settings).
Example of how it changes indentation
Suppose you define your function like this:
(defn my-custom-func {:style/indent 2} [x y z] (+ x y z))
When you call it across lines, your editor will format it like this (using 2-space indents):
(my-custom-func 10 20 30)
Without this metadata, the editor would use its default indentation for functions, which might look like this (aligning with the first argument):
(my-custom-func 10 20 30)
Key notes
- This metadata does not affect your function’s behavior at runtime. Clojure ignores it entirely when executing code—it’s purely for readability.
- You can use other values besides
2::style/indent :defntells the editor to indent calls just like it does for the built-indefn- A number like
1would indent subsequent lines by 1 unit instead of 2
- This pattern isn’t limited to
defn—you can add similar indentation metadata to macros too, since editors often need hints for macro call formatting.
内容的提问来源于stack exchange,提问作者Roman Podolski

