GridBagLayout ベースの JPanels の構築を支援する Clojure マクロに取り組んでいます。マクロ内のデフォルト マップで Java クラスを名前空間修飾に取得できますが、引数として渡されたものは取得できません。逆引用符、引用符、チルダ、またはその他の魔法の組み合わせが必要ですか?
(import [java.awt GridBagConstraints GridBagLayout Insets]
[javax.swing JButton JPanel])
(defmacro make-constraints [gridx gridy & constraints]
(let [defaults
{:gridwidth 1 :gridheight 1 :weightx 0 :weighty 0
:anchor 'GridBagConstraints/WEST :fill 'GridBagConstraints/NONE
:insets `(Insets. 5 5 5 5) :ipadx 0 :ipady 0}
values
(assoc (merge defaults (apply hash-map constraints))
:gridx gridx :gridy gridy)]
`(GridBagConstraints. ~@(map (fn [value]
(if
(or
(number? value)
(string? value)
(char? value)
(true? value)
(false? value)
(nil? value))
value
`~value))
(map values
[:gridx :gridy :gridwidth :gridheight
:weightx :weighty :anchor :fill
:insets :ipadx :ipady])))))
Insets
デフォルトマップで定義されたを使用すると、次のように修飾されます(「シンボルキャプチャ」ではありません) (java.awt.Insets ...)
:
user=> (macroexpand-1 '(make-constraints 0 0 :weightx 1))
(java.awt.GridBagConstraints.
0 0 1 1 1 0
GridBagConstraints/WEST GridBagConstraints/NONE
(java.awt.Insets. 5 5 5 5) 0 0)
しかし、引数として渡すと、そうではありません:
user=> (macroexpand-1 '(make-constraints 1 1 :insets (Insets. 2 2 2 2)))
(java.awt.GridBagConstraints.
1 1 1 1 0 0
GridBagConstraints/WEST GridBagConstraints/NONE
(Insets. 2 2 2 2) 0 0)
私は単に執着者になろうとしているのではありません。GridBagConstraints
適切なコンストラクターが見つからないというコンパイラ エラーが発生します。