8.4 Lazy Data-structure 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

▼ | 8 | Contracts

8.1 | Data-structure Contracts
8.2 | Function Contracts
8.3 | Parametric Contracts
8.4 | Lazy Data-structure Contracts
8.5 | Structure Type Property Contracts
8.6 | Attaching Contracts to Values
8.7 | Building New Contract Combinators
8.8 | Contract Utilities
8.9 | racket/ contract/ base
8.10 | Collapsible Contracts
8.11 | Legacy Contracts
8.12 | Random generation

On this page:

contract-struct
define-contract-struct

top contents ← prev up next →

#### 8.4`` Lazy Data-structure Contracts🔗ℹ

> syntax (contract-struct id (field-id ...))

> NOTE: This library is deprecated; use struct, instead. Lazy struct contracts no longer require a separate struct declaration; instead struct/dc and struct/c work directly with struct and define-struct.

Like struct, but with two differences: they do not define field mutators, and they define two contract constructors: id/c and id/dc. The first is a procedure that accepts as many arguments as there are fields and returns a contract for struct values whose fields match the arguments. The second is a syntactic form that also produces contracts on the structs, but the contracts on later fields may depend on the values of earlier fields.

The generated contract combinators are lazy: they only verify the contract holds for the portion of some data structure that is actually inspected. More precisely, a lazy data structure contract is not checked until a selector extracts a field of a struct.

> > (id/dc field-spec ...)
> > field-spec |  | = |  | [field-id contract-expr]
> > |  | | |  | [field-id (field-id ...) contract-expr]

> In each field-spec case, the first field-id specifies which field the contract applies to; the fields must be specified in the same order as the original contract-struct. The first case is for when the contract on the field does not depend on the value of any other field. The second case is for when the contract on the field does depend on some other fields, and the parenthesized field-ids indicate which fields it depends on; these dependencies can only be to earlier fields.

> syntax (define-contract-struct id (field-id ...))

> NOTE: This library is deprecated; use struct, instead. Lazy struct contracts no longer require a separate struct declaration; instead struct/dc and struct/c work directly with struct and define-struct.

Like contract-struct, but where the constructor’s name is make-id, much like define-struct.

top contents ← prev up next →