▼ |  | Unstable: May Change Without Warning

1 | Guidelines for Developing unstable Libraries
2 | Automata: Compiling State Machines
3 | Bytes
4 | Contracts
5 | Contracts for Macro Subexpressions
6 | Debugging
7 | Definitions
8 | Errors
9 | Filesystem
10 | Find
11 | Flonums
12 | Futures
13 | Functions
14 | Hash Tables
15 | Interface-Oriented Programming for Classes
16 | Lazy Require
17 | Lists
18 | Logging
19 | Macro Testing
20 | Mark Parameters
21 | Match
22 | Open place expressions
23 | Options
24 | Parameter Groups
25 | Pretty-Printing
26 | Re-Contracting Identifiers
27 | Sandbox
28 | Sequences
29 | Strings
30 | Structs
31 | Struct Printing
32 | Syntax
33 | Temporal Contracts: Explicit Contract Monitors
34 | Unix Domain Sockets
35 | 2D Syntax
36 | GUI Libraries

On this page:

wrap-expr/ c

5.3.6
top ← prev up next →

### 5`` Contracts for Macro Subexpressions

Ryan Culpepper <ryanc@racket-lang.org>

This library is unstable; compatibility will not be maintained. See Unstable: May Change Without Warning for more information.

(require unstable/wrapc)

This library provides a procedure wrap-expr/c for applying contracts to macro subexpressions.

> > procedure

> > (wrap-expr/c |  | contract-expr |  |  |  |
> > |  | expr |  |  |  |
> > | [ | #:positive pos-blame |  |  |  |
> > |  | #:negative neg-blame |  |  |  |
> > |  | #:name expr-name |  |  |  |
> > |  | #:macro macro-name |  |  |  |
> > |  | #:context context]) |  | → |  | syntax?

> contract-expr : syntax?
> expr : syntax?

> pos-blame

> :

> (or/c syntax? string? module-path-index?
> 'from-macro 'use-site 'unknown)

> |  |  | = |  | 'use-site

> neg-blame

> :

> (or/c syntax? string? module-path-index?
> 'from-macro 'use-site 'unknown)

> |  |  | = |  | 'from-macro

> expr-name : (or/c identifier? symbol? string? #f) = #f
> macro-name : (or/c identifier? symbol? string? #f) = #f
> context : (or/c syntax? #f) = (current-syntax-context)

Returns a syntax object representing an expression that applies the contract represented by contract-expr to the value produced by expr.

The other arguments have the same meaning as for expr/c.

Examples:

> > (define-syntax (myparameterize1 stx)
> (syntax-case stx ()
> [(_ ([p v]) body)
> (with-syntax ([cp (wrap-expr/c
> #'parameter? #'p
> #:name "the parameter argument"
> #:context stx)])
> #'(parameterize ([cp v]) body))]))

> > (myparameterize1 ([current-input-port
> (open-input-string "(1 2 3)")])
> (read))

> '(1 2 3)

> > (myparameterize1 (['whoops 'something])
> 'whatever)

> the parameter argument of myparameterize1: broke its
> contract
> promised: parameter?
> produced: 'whoops
> in: parameter?
> contract from: top-level
> blaming: top-level
> at: eval:4.0

> > (module mod racket
> (require (for-syntax unstable/wrapc))
> (define-syntax (app stx)
> (syntax-case stx ()
> [(app f arg)
> (with-syntax ([cf (wrap-expr/c
> #'(-> number? number?)
> #'f
> #:name "the function argument"
> #:context stx)])
> #'(cf arg))]))
> (provide app))

> > (require 'mod)

> > (app add1 5)
> 6
> > (app add1 'apple)
> the function argument of app: contract violation
> expected: number?
> given: 'apple
> in: the 1st argument of
> (-> number? number?)
> contract from: top-level
> blaming: (quote mod)
> at: eval:8.0
> > (app (lambda (x) 'pear) 5)
> the function argument of app: broke its contract
> promised: number?
> produced: 'pear
> in: the range of
> (-> number? number?)
> contract from: top-level
> blaming: top-level
> at: eval:9.0

top ← prev up next →