8.12

3.3 Definition and Declaration Macros🔗ℹ

The defn.macro form defines a definition macro. It is similar to expr.macro in prefix form, except that the name must be an identifier (never an operator), and the result syntax object should represent a block, which is spliced into the definition context where the macro is used.

Here’s the classic def_five macro:

import:

  rhombus/meta open

defn.macro 'def_five $id':

  'def $id = 5'

> def_five v

> v

5

Declarations macros are written with decl.macro, and the block produced by expansion can use forms like import and export.

By distinguishing between expression macros, definition macros, and declaration macros, Rhombus can report errors for out-of-place uses earlier and more clearly than Racket.