Version: 5.2
6 Contracts for struct type properties
Produces a contract for struct type properties. When the contract is
applied to a struct type property, it produces a wrapped struct type
property that applies
value-contract to the value associated
with the property when used to create a new struct type (via
struct,
make-struct-type, etc).
The struct type property’s accessor function is not affected; it must
be protected separately.
Examples: |
| | > (require 'propmod 'structmod) | > (define s1 (s even?)) | > (prop-app s1 5) | #f | > (prop-app s1 'apple) | prop-app: contract violation, expected: number?, given: | 'apple | contract from: propmod, blaming: top-level | contract: (-> prop? number? boolean?) | at: eval:2.0 | > (define s2 (s "not a fun")) | > (prop-app s2 5) | prop: contract violation, expected a procedure that accepts | 1 mandatory argument without any keywords, given: "not a | fun" | contract from: propmod, blaming: structmod | contract: | (struct-type-property/c | (-> prop? (-> number? boolean?))) | at: eval:2.0 | > (define s3 (s list)) | > (prop-app s3 5) | prop: contract violation, expected: boolean?, given: '(5) | contract from: propmod, blaming: structmod | contract: | (struct-type-property/c | (-> prop? (-> number? boolean?))) | at: eval:2.0 | > ((prop-ref s3) 'apple) | prop: self-contract violation, expected: prop?, given: | 'apple | contract from: propmod, blaming: propmod | contract: | (struct-type-property/c | (-> prop? (-> number? boolean?))) | at: eval:2.0 |
|
The first contract error above is a simple function contract violation
on prop-app. The second and third contract errors above blame
the structmod module, because it accepted the struct type
property contract. To avoid blame, structmod
should have placed a contract on s. The final contract error,
involving s3, blames propmod because the struct
type property contract obliges propmod to make sure the
property’s value is not misused, but propmod allows
direct access to the property value via prop-ref. To
avoid blame, propmod should remove the export of
prop-ref or protect it with a contract.