Skip to content

Commit e43e474

Browse files
committed
Add sections on :see-also and indentation metadata
1 parent f706dd3 commit e43e474

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

README.adoc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,67 @@ in the same namespace, and a fully-qualified name otherwise.
20082008

20092009
TIP: You can also consider adding `:supersedes` metadata to the newer APIs, basically the inverse of `:superseded-by`.
20102010

2011+
=== `:see-also`
2012+
2013+
From time to time you might want to point out some related vars/namespaces that the users of your library might be interested in.
2014+
The most common way to do so would be via the `:see-also` metadata, which takes a vector of related items.
2015+
When talking about vars - items in the same namespace don't need to fully qualified.
2016+
2017+
[source,clojure]
2018+
----
2019+
;; refers to vars in the same ns
2020+
(def ^{:see-also ["bar" "baz"]} foo
2021+
"A very useful var."
2022+
42)
2023+
2024+
;; refers to vars in some other ns
2025+
(defn ^{:see-also ["top.bar" "top.baz"]} foo
2026+
(bar))
2027+
----
2028+
2029+
NOTE: Many Clojure programming tools will also try to extract references to other vars from the docstring, but it's both
2030+
simpler and more explicit to use the `:see-also` metadata instead.
2031+
2032+
=== Indentation Metadata
2033+
2034+
Unlike other Lisp dialects, Clojure doesn't have a standard metadata format to specify the indentation of macros.
2035+
CIDER proposed a tool-agnostic https://docs.cider.mx/cider/indent_spec.html[indentation specification] based on metadata in 2015.footnote:[This was first introduced in CIDER 0.10] Here's a simple example:
2036+
2037+
[source,clojure]
2038+
----
2039+
;; refers to vars in the same ns
2040+
(defmacro with-in-str
2041+
"[DOCSTRING]"
2042+
{:style/indent 1}
2043+
[s & body]
2044+
...cut for brevity...)
2045+
----
2046+
2047+
This instructs the indentation engine that this is a macro with one ordinary parameter and a body after it.
2048+
2049+
[source,clojure]
2050+
----
2051+
;; without metadata (indented as a regular function)
2052+
(dop-iin-str some-string
2053+
foo
2054+
bar
2055+
baz)
2056+
2057+
;; with metadata (indented as macro with one special param and a body)
2058+
(with-in-str some-string
2059+
foo
2060+
bar
2061+
baz)
2062+
----
2063+
2064+
Unfortunately, as of 2020 there's still no widespread adoption of `:style/indent` and many editors and IDEs would just
2065+
hardcode the indentation rules for common macros.
2066+
2067+
NOTE: This approach to indentation ("semantic indentation") is a contested topic in the Clojure community, due to the
2068+
need for the additional metadata and tooling support. Despite the long tradition of that approach in the Lisp community
2069+
in general, some people argue to just stop treating functions and macros differently and simply indent everything with a fixed
2070+
indentation. https://tonsky.me/blog/clojurefmt/[This article] is one popular presentation of that alternative approach.
2071+
20112072
== Comments
20122073

20132074
[quote, Steve McConnell]

0 commit comments

Comments
 (0)