MacrosΒΆ

Dylan macros allow you to create new control constructs and other high-level forms. They can be used to automatically release resources, simplify class creation, or adapt Dylan for a specific problem domain.

Let’s say you find this code a little too verbose:

if (test())
  f(x)
else
  g(x)
end

and you’d rather be able to write it this way:

iff(test(), f(x), g(x))

You can’t just write iff as a function because then both f(x) and g(x) will be evaluated. The following macro will do the trick:

define macro iff
  { iff(?test:expression, ?true:expression, ?false:expression) }
  => { if (?test) ?true else ?false end }
end;

See also:

  • uncommon-dylan.dylan has a version of the iff macro that also accepts the syntax iff(test(), f(x)). Other macro examples in that file are inc!, dec!, and ignore-errors.
  • The define table macro in common-dylan is a simple example of making a new top-level “define” form.
  • Built-in macros – Many of the basic features of Dylan are implemented as macros.
  • The Macros chapter in the DRM.