[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 an expression position. Identifier macros are defined with define-id-macro:

  (define-id-macro name value-expr) 
The value-expr expression can evaluate to any value. When the identifier name is encountered during compilation, it is compiled as if the result of value-expr is in the place of name. Like define-macro, identifier macros defined with define-id-macro (at the top-level) are bound in the current namespace, and local identifier macros are defined with let-id-macro.

For example, the following 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.

(id-macro? v) returns #t if v is an identifier macro created with define-id-macro, #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.

A define-id-macro expression is treated specially by compile-file (see section 15.2.4).


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

PLT