Like a contract between two business partners, a software contract is an agreement between two parties. The agreement establishes obligations and guarantees for each “product” (or value) transferred from one party to another. A contract therefore sets a limit between the two parties. When a value exceeds this limit, the contract monitoring system conducts contractual checks to ensure that the partners comply with the established contract. All contracts and modules in this chapter (with the exception of the following modules) are written with the default syntax #lang for module descriptions. Because modules serve as a boundary between the parties in a contract, examples have several modules. In this example, the define/contract form sets a contract boundary between the amount definition and the surrounding context. In other words, the two parts here are the definition and the module it contains. Forms that set these nested contractual boundaries can sometimes be subtly used, as they can have unexpected effects on performance or accuse a party that appears unintuitive. These finesses are explained using definitions/contracts and > and contractual limits and define/contracts. (provide (Contract-out [amount (and/c? positive?)) promises all customers of the above module that the value of the amount will always be a positive number. The contractual system carefully monitors the commitment of the module.
Each time a customer returns the amount, the monitor verifies that the value of the amount is indeed a positive number. To experiment with multiple modules within a single module or in the DrRacket definition area, use the Racket submodules. For example, try for example earlier in this section: in this case, does the monitoring system apply positively? a symbol, but positive? reports an error because its domain consists only of numbers. In order for the contract to cover our intentions for all racketeering values, we can ensure that the value is both a number and a positive value by combining both contracts with and/c: in many cases it is useful to add contracts to modular limits. However, it is often convenient to be able to use contracts with finer granularity than modules. The Define/Contract form allows this type of use: if the module is needed, the monitoring system reports an infringement and accuses the module of having broken its promises. . If we link the amount to a figure that is not positive, the contract library is integrated into the racket language, but if you want to use racket / base, you can explicitly request the contract library as follows: Frequent short expressions: 1-400, 401-800, 801-1200, Plus An even more serious error would be to link the amount to a non-numerical value: each of the modules and their contracts are packed in square brackets with the module+ keyword on the front. The first form after the module is the name of the module that is to be used in a require statement below (each reference being preceded by a require the name “. .