8.2.6 Experimental: Contracts on macro sub-expressions

This section involves facilities that are experimental and subject to change.

Just as procedures often expect certain kinds of values as arguments, macros often have expectations about the expressions they are given. And just as procedures express those expectations via contracts, so can macros, using the expr/c syntax class.

For example, here is a macro myparameterize that behaves like parameterize but enforces the parameter? contract on the parameter expressions.

  > (define-syntax (myparameterize stx)
      (syntax-parse stx
        [(_ ((p v:expr) ...) body:expr)
         #:declare p (expr/c #'parameter?
                             #:name "parameter argument")
         #'(parameterize ((p.c v) ...) body)]))
  > (myparameterize ((current-input-port
                      (open-input-string "(1 2 3)")))
      (read))

  '(1 2 3)

  > (myparameterize (('whoops 'something))
      'whatever)

  self-contract violation: expected <parameter?>, given:

  'whoops

    contract on

      parameter argument of myparameterize

   from 'program, blaming 'program

    contract: parameter?

          at: eval:105.0

Important: Make sure when using expr/c to use the c attribute. If the macro above had used p in the template, the expansion would have used the raw, unchecked expressions. The expr/c syntax class does not change how pattern variables are bound; it only computes an attribute that represents the checked expression.