[Python-Dev] defmacro (was: Anonymous blocks)

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Apr 26 08:19:51 CEST 2005


Michael Chermside wrote:
> if the answer is that we want to prohibit nothing, then the right
> solution is macros.

I'm not sure about that. Smalltalk manages to provide very
reasonable-looking user-defined control structures without
using compile-time macros, just normal runtime evaluation
together with block arguments. It does this by starting
out with a fairly minimal and very flexible syntax.

This raises the question of why people feel the need for
macros in Lisp or Scheme, which have an even more minimal
and flexible syntax. I think part of the reason is that
the syntax for passing an unevaluated block is too obtrusive.
In Scheme you can define a function (not macro) that is
used like this:

   (with-file "foo/blarg"
      (lambda (f)
         (do-something-with f)))

But there is a natural tendency to want to be able to
cut out the lambda cruft and just write something like:

   (with-file "foo/blarg" (f)
      (do-something-with f))

and for that you need a macro.

The equivalent in Smalltalk would be something like

   File open: "foo/blarg" do: [:f f something]

which doesn't look too bad (compared to the rest of
the language!) because the block-passing syntax is
fairly unobtrusive.

So in summary, I don't think you necessarily *need*
macros to get nice-looking user-defined control structures.
It depends on other features of the language.

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+


More information about the Python-Dev mailing list