You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Clojure中(defn {:style/indent 2})语法含义及该配置作用咨询

Understanding {: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 :defn tells the editor to indent calls just like it does for the built-in defn
    • A number like 1 would 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:30:20