How to write a macro

0 views
Skip to first unread message

John Ky

unread,
Nov 11, 2009, 6:57:00 AM11/11/09
to clo...@googlegroups.com
Hi all,

I'm looking for a way to write a defkw macro so that (defkw ABSENT) expands to
(def ABSENT (kw "ABSENT" :ABSENT )).

Thanks,

-John

Alex Osborne

unread,
Nov 11, 2009, 8:40:03 AM11/11/09
to clo...@googlegroups.com
John Ky wrote:
> Hi all,
>
> I'm looking for a way to write a defkw macro so that (defkw ABSENT)
> expands to
> (def ABSENT (kw "ABSENT" :ABSENT )).
>

Just use `(...) as a template and use ~ to unescape, like so:

(defmacro defkw [sym]
`(def ~sym (kw ~(str sym) ~(keyword sym))))

(defkw ANSEMT)
=> (def ABSENT (kw "ABSENT" :ABSENT))

John Ky

unread,
Nov 11, 2009, 6:32:50 PM11/11/09
to clo...@googlegroups.com
Hi Alex,

I had to ~(keyword (str sym)) instead of ~(keyword sym), but now it works well.

Cheers,

-John

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Alex Osborne

unread,
Nov 11, 2009, 6:34:39 PM11/11/09
to clo...@googlegroups.com
John Ky wrote:
> I had to ~(keyword (str sym)) instead of ~(keyword sym), but now it
> works well.

Hmm, odd. Must have changed since Clojure 1.0. (keyword 'some-symbol)
works for me on the "new" branch.
Reply all
Reply to author
Forward
0 new messages