[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Macros: One expression => more than one expression?



I was playing around with classes in DrScheme last night, and I made a
number of classes that had one or more (init-field)s in them. After a while
I got tired of the typing, and defined a macro like this:

(require (lib "defmacro.ss"))

(define-macro (init-fields . fields)
  (map (lambda (x) `(init-field ,x)) fields))

Which wasn't quite right. What I wanted was to be able to type:

(class object%
	(init-fields foo bar baz))

and get:

(class object%
	(init-field foo)
	(init-field bar)
	(init-field baz))

But of course my macro produces:

(class object%
	((init-field foo)
	 (init-field bar)
	 (init-field baz)))

which doesn't work at all...

I can't see any way to have the one expression expand into multiple
expressions. Hopefully there's something obvious I'm missing here? I know I
could do it by making my own class macro that would handle (init-fields)
itself with unquote-splicing, but that's not my question. Can I make a macro
that expands into more than one expression, without any special support from
the enclosing context?

Thanks,

Bryn Keller
Senior Software Engineer
Jenkon
brk@jenkon.com