我有一个在 repl 中工作的宏,它似乎在我的代码之外let
和内部的代码中都可以工作,let
如下所示。然而它也坏了,我不知道为什么。宏基本上只是将第一个 arg 作为要调用的函数,并放在into-array
最后一组参数之前,如果它在 java 类中,它会将最终非集合参数的类作为参数添加到 into-array等级制度。
(defmacro jvar [method & args]
(let [lastargs (last args)
evaled-lastargs (eval lastargs)]
(if (coll? evaled-lastargs)
(let [firstargs (butlast args)
klass (eval (last firstargs))
supset (map #(supers (class %)) evaled-lastargs)
common (apply intersection #{klass} supset)]
(if (seq common)
`(~method ~@(butlast firstargs) (into-array ~(first common) ~lastargs))
`(~method ~@firstargs (into-array ~lastargs))))
(throw (Error. "Last argument must be defn.")))))
用法:在 Repl:
jfxcircles.core> (macroexpand '(jvar Group. Node [(Circle. 100)]))
(new Group (clojure.core/into-array javafx.scene.Node [(Circle. 100)]))
jfxcircles.core> (jvar Group. Node [(Circle. 100)])
#<Group Group@c26729e>
在let
:
(let [root (Group.)
scene (Scene. root 800 600) ;etc. this is for JavaFX
...
circ (Circle. 100)
inner-group1 (jvar Group. [(Circle. 100)]) ; works
inner-group2 (jvar Group. [circ]) ; Instantiation Exception
inner-group3 (jvar Group. [(Rectangle. 100 100)]) ; works
inner-group4 (jvar Group. [(Rectangle. (.getWidth scene) 100)]) ; Instantiation exception
)
所以基本上它不会在某些情况下编译,否则应该是相同的,我无法做宏扩展,因为它不会编译。有任何想法吗?
谢谢