实用 Clojure:宏和 Java 互操作
Practical Clojure: macros and Java interop
在 Practical Clojure 关于 Java 互操作的章节中,作者注意到以下关于 Java 互操作 "syntactic sugar"(例如 (.method object arguments)
而不是 (. 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.
我不明白为什么 "syntactic sugar" 扩展发生在与宏扩展相同的阶段是一个问题。是不是展开的顺序可能有问题?
与生成互操作调用有关的宏通常应该使用脱糖特殊形式,但这不是因为脱糖发生的时间,也不是问题。而且他们不必:我数不清的次数,我看到有人写道:
(defmacro call [obj method & args]
`(~(symbol (str "." (name method))) ~obj ~@args))
与使用适当工具的外观相比,这简直是一团糟:
(defmacro call [obj method & args]
`(. ~obj ~method ~@args))
在 Practical Clojure 关于 Java 互操作的章节中,作者注意到以下关于 Java 互操作 "syntactic sugar"(例如 (.method object arguments)
而不是 (. 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.
我不明白为什么 "syntactic sugar" 扩展发生在与宏扩展相同的阶段是一个问题。是不是展开的顺序可能有问题?
与生成互操作调用有关的宏通常应该使用脱糖特殊形式,但这不是因为脱糖发生的时间,也不是问题。而且他们不必:我数不清的次数,我看到有人写道:
(defmacro call [obj method & args]
`(~(symbol (str "." (name method))) ~obj ~@args))
与使用适当工具的外观相比,这简直是一团糟:
(defmacro call [obj method & args]
`(. ~obj ~method ~@args))