previous up next     contents index
Next: Expansion Time Binding and Up: Macros Previous: Defining Macros

Identifier Macros

  An identifier macro is a macro that is not ``applied'' to syntactic arguments. Instead, an identifier macro identifier is directly replaced with its value whenever the identifier is in a value position. Identifier macros are defined with  define-id-macro:

 
  (define-id-macro name value) 
The value expression can evaluate to any value. When the identifier name is encountered during compilation, is is compiled as if value is in the place of name. Local identifier macros are defined with  let-id-macro.

For example, this expression uses x to automatically unbox the value in b:

 
   (let ([b (box 5)])
     (let-id-macro x `(unbox b)
       (display x) (newline)
       (set-box! b 8)
       (display x) (newline))) 
Each use of x is replaced by (unbox b), so this expression prints 5 and 8 to the current output port. The x identifier is not a variable; (set! x 8) is illegal, since this expands to (set! (unbox b) 8). The value of the identifier macro x is the S-expression '(unbox b). Leaving out the quote in defining x's value is illegal:
 
   (let ([b (box 5)])
     (let-id-macro x (unbox b)
       expr)) 
because the (unbox b) expression is evaluated at compile time and is not in the scope of b. (If b is a global variable bound to a box when the expression is compiled, then the expression is legal and the global b is used.)

As with let-macro, the  let-id-macro form defines a local idenfifier macro and an embedded define-id-macro expression is transformed into a let-id-macro expression.

The  id-macro? procedure returns #t if its argument is an identifier macro created with define-id-macro or #f otherwise. Note that id-macro? cannot be applied directly to identifier macro identifiers, but identifier macro values can be obtained indirectly with  global-defined-value.


previous up next     contents index
Next: Expansion Time Binding and Up: Macros Previous: Defining Macros

PLT