Version: 5.1.2
4 Contracts
This library is unstable;
compatibility will not be maintained.
See Unstable: May Change Without Warning for more information.
Returns #t if x is of the appropriate data type
(string, list, bytes, or vector, respectively) and is not empty;
returns #f otherwise.
Returns #t if x is a list of one element; returns
#f otherwise.
The subsequent bindings were added by Ryan Culpepper.
Produces a contract that, when applied to a value, first tests the
value with predicate; if predicate returns true, the
then-contract is applied; otherwise, the
else-contract is applied. The resulting contract is a flat
contract if both then-contract and else-contract are
flat contracts.
For example, the following contract enforces that if a value is a
procedure, it is a thunk; otherwise it can be any (non-procedure)
value:
Note that the following contract is not equivalent:
The last contract is the same as
any/c because
or/c tries flat contracts before higher-order contracts.
A contract that describes the failure result arguments of procedures
such as
hash-ref.
Equivalent to (if/c procedure? (-> any) any/c).
Produces a contract that acts like contract but with the name
name.
The resulting contract is a flat contract if contract is a
flat contract.
The subsequent bindings were added by Asumu Takikawa.
Creates a contract that acts like contract but will also
accept #f. Intended to describe situations where a failure
or default value may be used.
The subsequent bindings were added by Carl Eastlund <cce@racket-lang.org>.
4.1 Flat Contracts
This contract recognizes Scheme truth values, i.e., any value, but with a more
informative name and description. Use it in negative positions for arguments
that accept arbitrary truth values that may not be booleans.
4.2 Syntax Object Contracts
Recognizes syntax objects
stx such that
(syntax->datum stx)
satisfies
datum/c.
4.3 Higher-Order Contracts
These contracts recognize functions that accept 0, 1, or 2 arguments,
respectively, and produce a single result.
These contracts recognize predicates: functions of a single argument that
produce a boolean result.
The first constrains its output to satisfy boolean?. Use
predicate/c in positive position for predicates that guarantee a result
of #t or #f.
The second constrains its output to satisfy truth/c. Use
predicate-like/c in negative position for predicates passed as
arguments that may return arbitrary values as truth values.
These contracts recognize comparisons: functions of two arguments that
produce a boolean result.
The first constrains its output to satisfy boolean?. Use
comparison/c in positive position for comparisons that guarantee a
result of #t or #f.
The second constrains its output to satisfy truth/c. Use
comparison-like/c in negative position for comparisons passed as
arguments that may return arbitrary values as truth values.
Wraps a
sequence,
obligating it to produce as many values as there are
elem/c contracts,
and obligating each value to satisfy the corresponding
elem/c. The
result is not guaranteed to be the same kind of sequence as the original value;
for instance, a wrapped list is not guaranteed to satisfy
list?.
Examples: |
| > (for ([P predicates]) | (printf "~s\n" (P "cat"))) |
| #f | predicates: self-contract violation, expected <boolean?>, | given: 'cat | contract from (definition predicates), blaming | (definition predicates) | contract: | (sequence/c (-> any/c boolean?)) | at: eval:2.0 |
|
Wraps a
dictionary,
obligating its keys to satisfy
key/c and their corresponding values to
satisfy
value/c. The result is not guaranteed to be the same kind of
dictionary as the original value; for instance, a wrapped hash table is not
guaranteed to satisfy
hash?.
Examples: |
| > (dict-ref table 'A) | "A" | > (dict-ref table 'B) | table: self-contract violation, expected <string?>, given: 2 | contract from (definition table), blaming (definition | table) | contract: (dict/c symbol? string?) | at: eval:4.0 | > (dict-ref table 3) | table: contract violation, expected <symbol?>, given: 3 | contract from top-level, blaming (definition table) | contract: (dict/c symbol? string?) | at: eval:4.0 |
|
Warning: Bear in mind that key and value contracts are re-wrapped on
every dictionary operation, and dictionaries wrapped in dict/c multiple
times will perform the checks as many times for each operation. Especially for
immutable dictionaries (which may be passed through a constructor that involves
dict/c on each update), contract-wrapped dictionaries may be much less
efficient than the original dictionaries.