4

I've been writing a clojure parser and come across the follow syntax:

(defn key
  "Returns the key of the map entry."
  {:added "1.0"
   :static true}
  [^java.util.Map$Entry e]
    (. e (getKey)))

What does that '$' mean here? Is there any usage for that kind of syntax outside of metadata?

Pavel Murygin
  • 2,242
  • 2
  • 18
  • 26

1 Answers1

6

This is the way to access nested classes in Clojure. In this case, you're accessing Entry, which is an interface defined inside the Map interface

In Java, you'd simply write java.util.Map.Entry, in Clojure you need to use the dollar sign: java.util.Map$Entry

From the Clojure documentation on Java interoperability:

The '.' special form is the basis for access to Java. It can be considered a member-access operator, and/or read as 'in the scope of'.

If the first operand is a symbol that resolves to a class name, the access is considered to be to a static member of the named class. Note that nested classes are named EnclosingClass$NestedClass, per the JVM spec. Otherwise it is presumed to be an instance member and the first argument is evaluated to produce the target object.

Community
  • 1
  • 1
toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131