문제

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)
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top