improved overview a little
svn: r12027
This commit is contained in:
parent
d3397103a8
commit
7ace3962fa
|
@ -9,15 +9,33 @@ and pragmatics of using contracts. See
|
||||||
@guidesecref["contracts"] in the Guide for more of the
|
@guidesecref["contracts"] in the Guide for more of the
|
||||||
latter and less of the former.
|
latter and less of the former.
|
||||||
|
|
||||||
A @defterm{contract} controls the flow of values to ensure that the
|
The contract system guards one part of a program from
|
||||||
expectations of one party are met by another party. The
|
another. Programmers specify the behavior of a module exports via
|
||||||
@scheme[provide/contract] form is the primary mechanism for
|
@scheme[provide/contract] and the contract system enforces those
|
||||||
associating a contract with a binding.
|
constraints.
|
||||||
|
|
||||||
Note that all of the combinators that accept contracts as arguments
|
@deftech{Contracts} come in two forms: those constructed by the
|
||||||
use @scheme[coerce-contract], meaning that symbols, booleans, strings,
|
various operations listed in this section of the manual, and various
|
||||||
bytess, characters, numbers, regular expressions, and predicates
|
ordinary Scheme values double as contracts, including
|
||||||
are all implicitly converted into contracts.
|
@itemize{
|
||||||
|
@item{@tech{symbols}, @tech{booleans}, @tech{characters}, and
|
||||||
|
@scheme[null], which are treated as contracts that recognize
|
||||||
|
themselves, using @scheme[eq?], }
|
||||||
|
|
||||||
|
@item{@tech{strings} and @tech{bytes strings}, which are treated as contracts
|
||||||
|
that recognize themselves using @scheme[equal?], }
|
||||||
|
|
||||||
|
@item{@tech{numbers}, which are treated as contracts
|
||||||
|
that recognize themselves using @scheme[=],}
|
||||||
|
|
||||||
|
@item{@tech{regular expressions}, which are treated as contracts that recognize @tech{byte strings} and @tech{strings} that match the regular expression, and }
|
||||||
|
|
||||||
|
@item{predicates: any procedure whose arity is 1 is treated as a
|
||||||
|
predicate. During contract checking, it is applied to the values that
|
||||||
|
appear and should return @scheme[#f] to indicate that the contract
|
||||||
|
failed, and anything else to indicate it passed.}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@note-lib[scheme/contract #:use-sources (scheme/private/contract-ds
|
@note-lib[scheme/contract #:use-sources (scheme/private/contract-ds
|
||||||
scheme/private/contract
|
scheme/private/contract
|
||||||
|
@ -905,19 +923,12 @@ extracts the names from any contracts it is supplied with.}
|
||||||
|
|
||||||
@defproc[(coerce-contract [id symbol?] [x any/c]) contract?]{
|
@defproc[(coerce-contract [id symbol?] [x any/c]) contract?]{
|
||||||
|
|
||||||
If @scheme[x] is a contract, it returns it. If it is a procedure of
|
Converts a regular scheme value into an instance of a contract struct,
|
||||||
arity one, it converts that into a contract by treating the result as
|
converting it according to the description of @tech{contracts}.
|
||||||
a predicate. If it is a symbol, boolean, or character, it makes a
|
|
||||||
contract that accepts values that are @scheme[eq?] to @scheme[x]. If
|
|
||||||
@scheme[x] is a string or a bytes, it makes a contract that
|
|
||||||
accespts values that are @scheme[equal?] to @scheme[x]. If @scheme[x]
|
|
||||||
is a regular expression or a byte regular expression, it makes a
|
|
||||||
contract that accepts strings and bytes, as long as they match the
|
|
||||||
regular expression.
|
|
||||||
|
|
||||||
If @scheme[x] is none of the above, @scheme[coerce-contract]
|
If @scheme[x] is not one of the coercable values,
|
||||||
signals an error, using the first argument in the error
|
@scheme[coerce-contract] signals an error, using the first argument in
|
||||||
message.}
|
the error message.}
|
||||||
|
|
||||||
@defproc[(coerce-contracts [id symbol?] [xs (listof any/c)]) (listof contract?)]{
|
@defproc[(coerce-contracts [id symbol?] [xs (listof any/c)]) (listof contract?)]{
|
||||||
|
|
||||||
|
@ -986,9 +997,13 @@ otherwise.}
|
||||||
|
|
||||||
@defproc[(flat-contract? [v any/c]) boolean?]{
|
@defproc[(flat-contract? [v any/c]) boolean?]{
|
||||||
|
|
||||||
Returns @scheme[#t] when its argument is a contract that has been
|
Returns @scheme[#t] when its argument is a contract that can be
|
||||||
constructed with @scheme[flat-contract] (and thus is essentially just
|
checked immediately (unlike, say, a function contract).
|
||||||
a predicate), @scheme[#f] otherwise.}
|
|
||||||
|
For example,
|
||||||
|
@scheme[flat-contract] constructs flat contracts from predicates, and
|
||||||
|
symbols, booleans, numbers, and other ordinary Scheme values are also
|
||||||
|
flat contracts.}
|
||||||
|
|
||||||
@defproc[(flat-contract-predicate [v flat-contract?])
|
@defproc[(flat-contract-predicate [v flat-contract?])
|
||||||
(any/c . -> . any/c)]{
|
(any/c . -> . any/c)]{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user