I need to add several methods to a Clojure defprotocol that I am writing for several identical Swing components:
(defprotocol view-methods
(ok-button-add-action-listener [this listener])
(ok-button-set-enabled [this enabled])
(ok-button-set-selected [this selected])
(cancel-button-add-action-listener [this listener])
(cancel-button-set-enabled [this enabled])
(cancel-button-set-selected [this selected])
(other-button-add-action-listener [this listener])
(other-button-set-enabled [this enabled])
(other-button-set-selected [this selected]))
Is there any way that I can write a macro that returns all three of the method signatures (xxx-button-add-action-listener, xxx-button-set-enabled, xxx-button-set-selected)?
(defprotocol view-methods
(add-methods ok)
(add-methods cancel)
(add-methods other))
This macro needs to add three items to the growing defprotocol with each invocation.
Can a macro return `~@a-list and expand “in place”?
Yes, you just need to have your macro expand in a
(do ...), and the Clojure compiler will thread thedochildren as a sequence of top level forms.