On this page:
struct-type-property/ c
Version: 5.1.1

5 Contracts for struct type properties

Ryan Culpepper <ryanc@racket-lang.org>

 (require unstable/prop-contract)

This library is unstable; compatibility will not be maintained. See Unstable for more information.

(struct-type-property/c value-contract)  contract?
  value-contract : contract?
Produces a contract for struct type properties. When the contract is applied to a struct type property, it produces a wrapped struct type property. When the wrapped struct type property is used to create a new struct type (via struct, make-struct-type, etc), it applies value-contract to the value associated with the property.

The contract has no effect on the struct type property accessor.

Examples:

> (define-values (prop prop? prop-ref)
    (make-struct-type-property 'prop))
> (define/contract wrapped
      (struct-type-property/c (-> any/c (-> number? number?)))
    prop)
> (struct s (f)
    #:property wrapped (lambda (s) (s-f s)))
> (define (get-f s) ((prop-ref s) s))
> (define s1 (s add1))
> ((get-f s1) 5)

6

> ((get-f s1) 'apple)

self-contract violation: expected <number?>, given: 'apple

  contract on wrapped from (definition wrapped), blaming

(definition wrapped)

  contract:

    (struct-type-property/c

    (-> any/c (-> number? number?)))

  at: eval:3.0

> (define s2 (s (lambda (n) (if (zero? n) 'zero 'nonzero))))
> ((get-f s2) 5)

contract violation: expected <number?>, given: 'nonzero

  contract on wrapped from (definition wrapped), blaming

top-level

  contract:

    (struct-type-property/c

    (-> any/c (-> number? number?)))

  at: eval:3.0

> ((get-f s2) 'apple)

self-contract violation: expected <number?>, given: 'apple

  contract on wrapped from (definition wrapped), blaming

(definition wrapped)

  contract:

    (struct-type-property/c

    (-> any/c (-> number? number?)))

  at: eval:3.0