Question

From the Clojure docs, this is how to access a static field of a Java class:

Classname/staticField

Math/PI
-> 3.141592653589793

And this is the expansion:

The expansions are as follows:

   Classname/staticField ==> (. Classname staticField)

I can't get this to expand using macroexpand*:

> (macroexpand 'Math/E)
Math/E

What do I use to expand Classname/staticField?

This is Clojure v1.6.0.


*Although this does work:

> (macroexpand '(Math/E))
(. Math E)
Was it helpful?

Solution

The documentation is a little bit inaccurate in that respect. Macro expansion only applies to list forms, not to bare symbols, so only the first three special forms listed (instance methods on objects and classes, static methods on classes) are handled at macro expansion time. The Classname/staticField syntax is resolved to a static field access after macro expansion, when symbols are resolved to vars, classes, or let-bound names as described in http://clojure.org/evaluation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top