1

We know that Clojure is a Lisp-1, that is, there are no separate function and variable namespaces.

I am coming at this from perspective of Common Lisp (which is a Lisp-2).

I'm seeking to understand metadata and am confused by the following:

user=> (def ^Integer my 3)
#'user/my
user=> (meta my)
nil
user=> (meta #'my)
{:tag java.lang.Integer, :line 1, :column 1, :file "NO_SOURCE_FILE", :name my, :ns #object[clojure.lang.Namespace 0x1d5a857d "user"]}

user=> (def myatom (atom (for [x [1 2 3]] ^{:key 'foo} x)))
#'user/myatom
user=> @myatom
(1 2 3)
user=> (first @myatom)
1
user=> (meta (first @myatom))
nil
user=> (meta #'(first @myatom))

RuntimeException EOF while reading, starting at line 1  clojure.lang.Util.runtimeException (Util.java:221)

RuntimeException Unmatched delimiter: )  clojure.lang.Util.runtimeException (Util.java:221)

What's the role of the sharp quote #' in Clojure?


Also, regarding metadata, I'm a bit confused by the following:

Children: [:ul [:li] [:li] [:li] [:li]]
Keyed: [:ul [^3[:li] ^4[:li] ^5[:li] ^6[:li]]]

This is from the clojurescript/reagent chapter in the book Professional Clojure. Is the second line ordinary Clojure metadata syntax, or is that only valid as a result of a reagent macro?

Some more experiments at the repl:

user=> ^1[2]
IllegalArgumentException Metadata must be Symbol,Keyword,String or Map  clojure.lang.LispReader$MetaReader.invoke (LispReader.java:798)
user=> ^:a[1]
[2][1]
user=> ^:a[1]
[1]
user=> (meta ^:a[1])
{:a true}
user=> 

The second line there seems odd, but perhaps never mind that in the first instance unless it helps.

mwal
  • 2,803
  • 26
  • 34

1 Answers1

5

Using the sharp-quote like #'my is shorthand for writing (var my).

var returns the Var object which the symbol my points to. Note that var is a Clojure special form, and does not behave like a regular function call.

Please see this question for full details on symbols, vars, and values in Clojure: When to use a Var instead of a function?

See also:

Alan Thompson
  • 29,276
  • 6
  • 41
  • 48