2

In Practical Clojure's chapter on Java interop, the authors note the following about the Java interop "syntactic sugar" (e.g. (.method object arguments) instead of (. object method arguments)):

Since these “syntactic sugar” expansions happen in the same compilation phase as macro-expansion, macros that do complex code-generation may need to avoid them and use the new and . (dot) special forms directly.

I don't understand why "syntactic sugar" expansion happening in the same phase as macro expansion is a problem. Is it because there may be issues with the order of expansions?

2
  • 2
    What exactly is your question? Nowhere in the text you quoted is the word "problem" or "issue" mentioned. Could you elaborate more on what you mean, preferably with an example? Commented Feb 21, 2016 at 22:23
  • "May need to avoid them" suggests a problem, but as @amalloy has pointed out, it's not a technical limitation. Commented Feb 22, 2016 at 15:52

1 Answer 1

4

Macros concerned with generating interop calls typically should use the desugared special form, but that's not because of when desugaring happens, nor is it a problem. And they don't have to: more times than I care to count, I've seen someone write:

(defmacro call [obj method & args]
  `(~(symbol (str "." (name method))) ~obj ~@args))

which is just a total mess, compared to how it would look with the appropriate tool:

(defmacro call [obj method & args]
  `(. ~obj ~method ~@args))
Sign up to request clarification or add additional context in comments.

1 Comment

OK, it's a style and good practice issue then, as opposed to a technical limitation. The authors didn't make that clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.