► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

▼ | 3 | Syntactic Forms

3.1 | Modules: module, module*, ...
3.2 | Importing and Exporting: require and provide
3.3 | Literals: quote and #%datum
3.4 | Expression Wrapper: #%expression
3.5 | Variable References and #%top
3.6 | Locations: #%variable-reference
3.7 | Procedure Applications and #%app
3.8 | Procedure Expressions: lambda and case-lambda
3.9 | Local Binding: let, let*, letrec, ...
3.10 | Local Definitions: local
3.11 | Constructing Graphs: shared
3.12 | Conditionals: if, cond, and, and or
3.13 | Dispatch: case
3.14 | Definitions: define, define-syntax, ...
3.15 | Sequencing: begin, begin0, and begin-for-syntax
3.16 | Guarded Evaluation: when and unless
3.17 | Assignment: set! and set!-values
3.18 | Iterations and Comprehensions: for, for/ list, ...
3.19 | Continuation Marks: with-continuation-mark
3.20 | Quasiquoting: quasiquote, unquote, and unquote-splicing
3.21 | Syntax Quoting: quote-syntax
3.22 | Interaction Wrapper: #%top-interaction
3.23 | Blocks: block
3.24 | Internal-Definition Limiting: #%stratified-body
3.25 | Performance Hints: begin-encourage-inline
3.26 | Importing Modules Lazily: lazy-require

On this page:

#%expression

top ← prev up next →

#### 3.4`` Expression Wrapper: #%expression

> > syntax

> > (#%expression expr)

Produces the same result as expr. Using #%expression forces the parsing of a form as an expression.

Examples:

> > (#%expression (+ 1 2))
> 3
> > (#%expression (define x 10))
> eval:8:0: define: not allowed in an expression context
> in: (define x 10)

The #%expression form is helpful in recursive definition contexts where expanding a subsequent definition can provide compile-time information for the current expression. For example, consider a define-sym-case macro that simply records some symbols at compile-time in a given identifier.

> (define-syntax (define-sym-case stx)
> (syntax-case stx ()
> [(_ id sym ...)
> (andmap identifier? (syntax->list #'(sym ...)))
> #'(define-syntax id
> '(sym ...))]))

and then a variant of case that checks to make sure the symbols used in the expression match those given in the earlier definition:

> (define-syntax (sym-case stx)
> (syntax-case stx ()
> [(_ id val-expr [(sym) expr] ...)
> (let ()
> (define expected-ids
> (syntax-local-value
> #'id
> (λ ()
> (raise-syntax-error
> 'sym-case
> "expected an identifier bound via define-sym-case"
> stx
> #'id))))
> (define actual-ids (syntax->datum #'(sym ...)))
> (unless (equal? expected-ids actual-ids)
> (raise-syntax-error
> 'sym-case
> (format "expected the symbols ~s"
> expected-ids)
> stx))
> #'(case val-expr [(sym) expr] ...))]))

If the definition follows the use like this, then the define-sym-case macro does not have a chance to bind id and the sym-case macro signals an error:

> > (let ()
> (sym-case land-creatures 'bear
> [(bear) 1]
> [(fox) 2])
> (define-sym-case land-creatures bear fox))

> eval:11:0: sym-case: expected an identifier bound via
> define-sym-case
> at: land-creatures
> in: (sym-case land-creatures (quote bear) ((bear) 1)
> ((fox) 2))

But if the sym-case is wrapped in an #%expression, then the expander does not need to expand it to know it is an expression and it moves on to the define-sym-case expression.

> > (let ()
> (#%expression (sym-case sea-creatures 'whale
> [(whale) 1]
> [(squid) 2]))
> (define-sym-case sea-creatures whale squid)
> 'more...)

> 'more...

Of course, a macro like sym-case should not require its clients to add #%expression; instead it should check the basic shape of its arguments and then expand to #%expression wrapped around a helper macro that calls syntax-local-value and finishes the expansion.
top ← prev up next →