7.9 Unit Contracts

► |  | 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

▼ | 7 | Units

7.1 | Creating Units
7.2 | Invoking Units
7.3 | Linking Units and Creating Compound Units
7.4 | Inferred Linking
7.5 | Generating A Unit from Context
7.6 | Structural Matching
7.7 | Extending the Syntax of Signatures
7.8 | Unit Utilities
7.9 | Unit Contracts
7.10 | Single-Unit Modules
7.11 | Single-Signature Modules
7.12 | Transformer Helpers

On this page:

unit/ c
define-unit/ contract

top contents ← prev up next →

#### 7.9`` Unit Contracts🔗ℹ

> > syntax
> > (unit/c
> > (import sig-spec-block ...)
> > (export sig-spec-block ...)
> > init-depends-decl
> > optional-body-ctc)
> sig-spec-block |  | = |  | (tagged-sig-spec [id contract] ...)
> |  | | |  | tagged-sig-spec
> init-depends-decl |  | = |  |
> |  | | |  | (init-depend tagged-sig-id ...)
> optional-body-ctc |  | = |  |
> |  | | |  | contract
> |  | | |  | (values contract ...)

A unit contract wraps a unit and checks both its imported and exported identifiers to ensure that they match the appropriate contracts. This allows the programmer to add contract checks to a single unit value without adding contracts to the imported and exported signatures.

The unit value must import a subset of the import signatures and export a superset of the export signatures listed in the unit contract. Additionally, the unit value must declare initialization dependencies that are a subset of those specified in the unit contract. Any identifier which is not listed for a given signature is left alone. Variables used in a given contract expression first refer to other variables in any of the listed signatures, and then to the context of the unit/c expression. If a body contract is specified then the result of invoking the unit value is wrapped with the given contract, otherwise the values are returned as-is.

Changed in version 8.8.0.7 of package base: Changed sig-spec-block to allow arbitrary tagged-sig-specs instead of only allowing tagged-sig-ids. Made bindings from all signatures visible in the scope of each contract expression instead of only the bindings from the same signature. Additionally, contracts on signature bindings are enforced within contract expressions.

> > syntax
> > (define-unit/contract unit-id
> > (import sig-spec-block ...)
> > (export sig-spec-block ...)
> > init-depends-decl
> > optional-body-ctc
> > unit-body-expr-or-defn
> > ...)
> sig-spec-block |  | = |  | (tagged-sig-spec [id contract] ...)
> |  | | |  | tagged-sig-spec
> optional-body-ctc |  | = |  |
> |  | | |  | #:invoke/contract contract
> |  | | |  | #:invoke/contract (values contract ...)

The define-unit/contract form defines a unit compatible with link inference whose imports and exports are contracted with a unit contract. The unit name is used for the positive blame of the contract.

Changed in version 8.8.0.7 of package base: Made bindings from all signatures visible in the scope of each contract expression instead of only the bindings from the same signature. Additionally, contracts on signature bindings are enforced within contract expressions.

top contents ← prev up next →