8.8 Contract Utilities
procedure
(chaperone-contract? v) → boolean?
v : any/c
procedure
v : any/c
procedure
(flat-contract? v) → boolean?
v : any/c
For example, flat-contract constructs flat contracts from predicates, and symbols, booleans, numbers, and other ordinary Racket values (that are defined as contracts) are also flat contracts.
procedure
(list-contract? v) → boolean?
v : any/c
A list contract is one that insists that its argument is a list?, meaning that the value cannot be cyclic and must either be the empty list or a pair constructed with cons and another list.
Added in version 6.0.1.13 of package base.
procedure
(contract-name c) → any/c
c : contract?
procedure
(value-contract v) → (or/c contract? #f)
v : has-contract?
To support value-contract and value-contract in your own contract combinators, use prop:contracted or impersonator-prop:contracted.
procedure
(has-contract? v) → boolean?
v : any/c
procedure
(value-blame v) → (or/c blame? #f)
v : has-blame?
To support value-contract and value-blame in your own contract combinators, use prop:blame or impersonator-prop:blame.
Added in version 6.0.1.12 of package base.
procedure
(has-blame? v) → boolean?
v : any/c
Added in version 6.0.1.12 of package base.
The first argument, blame? object encapsulates information about the contract checking, mostly used to create a meaningful error message if a contract violation is detected. The resulting function’s first argument is the value that should have the contract and its second argument is a missing party for the blame object, to be passed to raise-contract-error.
If possible, use this function instead of contract-val-first-projection or contract-projection.
procedure
(make-none/c sexp-name) → contract?
sexp-name : any/c
syntax
(recursive-contract contract-expr recursive-contract-option ...)
(recursive-contract contract-expr type recursive-contract-option ...)
recursive-contract-option = #:list-contract? | #:extra-delay type = #:impersonator | #:chaperone | #:flat
If the recursive-contract-option #:list-contract? is given, then the result is a list-contract? and the contract-expr must evaluate to a list-contract?.
If the recursive-contract-option #:extra-delay is given, then the contract-expr expression is evaluated only when the first value to be checked against the contract is supplied to the contract. Without it, the contract-expr is evaluated earlier. This option is supported only when type is #:flat.
> (define even-length-list/c (or/c null? (cons/c any/c (cons/c any/c (recursive-contract even-length-list/c #:flat))))) > (even-length-list/c '(A B)) #t
> (even-length-list/c '(1 2 3)) #f
Changed in version 6.0.1.13 of package base: Added the #:list-contract? option.
Changed in version 6.7.0.3: Added the #:extra-delay option.
syntax
(opt/c contract-expr maybe-name)
maybe-name =
| #:error-name id
If the #:error-name argument is present, and contract-expr evaluates to a non-contract expression, then opt/c raises an error using id as the name of the primitive, instead of using the name opt/c.
> (define/contract (f x) (opt/c '(not-a-contract)) x) opt/c: contract violation
expected: contract?
given: '(not-a-contract)
> (define/contract (f x) (opt/c '(not-a-contract) #:error-name define/contract) x) define/contract: contract violation
expected: contract?
given: '(not-a-contract)
syntax
(define-opt/c (id id ...) expr)
For example,
(define-contract-struct bt (val left right)) (define-opt/c (bst-between/c lo hi) (or/c null? (bt/c [val (real-in lo hi)] [left (val) (bst-between/c lo val)] [right (val) (bst-between/c val hi)]))) (define bst/c (bst-between/c -inf.0 +inf.0))
defines the bst/c contract that checks the binary search tree invariant. Removing the -opt/c also makes a binary search tree contract, but one that is (approximately) 20 times slower.
Note that in some cases, a call to a function defined by define-opt/c may terminate, even if the corresponding define-based function would not terminate. This is a shortcoming in define-opt/c that we hope to understand and fix at some point, but have no concrete plans currently.
Added in version 6.4.0.4 of package base.
procedure
(contract-custom-write-property-proc c p mode) → void? c : contract? p : output-port? mode : (or/c #f #t 0 1)
Added in version 6.1.1.5 of package base.
procedure
(rename-contract contract name) → contract?
contract : contract? name : any/c
The resulting contract is a flat contract if contract is a flat contract.
Added in version 6.3 of package base.
This will only return #t in the dynamic extent of or/c or first-or/c’s checking to determine which branch to use.
Added in version 6.3.0.9 of package base.
If not in the dynamic-extent of or/c’s or first-or/c’s checking to determine the branch, then this form has no effect.
Added in version 6.3.0.9 of package base.
(if/c procedure? (-> any) any/c)
Added in version 6.3 of package base.
value
Equivalent to (if/c procedure? (-> any) any/c).
Added in version 6.3 of package base.