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 the do
children as a sequence of top level forms.
I believe a macro must expand to a single form - so you can't do this in the exact way you are describing.
However, all is not lost because it would certainly be possible to write this with a macro at the top level that looks something like the following:
(defmacro build-button-protocol [name & method-specs]
....)
Which you could use as follows:
(build-button-protocol view-methods
(add-methods ok)
(add-methods cancel)
(add-methods other))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With