On this page:
unit/  c
define-unit/  contract

7.9 Unit Contracts

syntax

(unit/c
  (import sig-block ...)
  (export sig-block ...)
  init-depends-decl
  optional-body-ctc)
 
sig-block = (tagged-sig-id [id contract] ...)
  | tagged-sig-id
     
init-depends-decl = 
  | (init-depend tagged-sig-id ...)
     
optional-body-ctc = 
  | contract
  | (values contract ...)
A unit contract wraps a unit and checks both its imported and exported identifiers to ensure that they match the appropriate contracts. This allows the programmer to add contract checks to a single unit value without adding contracts to the imported and exported signatures.

The unit value must import a subset of the import signatures and export a superset of the export signatures listed in the unit contract. Additionally, the unit value must declare initialization dependencies that are a subset of those specified in the unit contract. Any identifier which is not listed for a given signature is left alone. Variables used in a given contract expression first refer to other variables in the same signature, and then to the context of the unit/c expression. If a body contract is specified then the result of invoking the unit value is wrapped with the given contract, if no body contract is supplied then no wrapping occurs when the unit value is invoked.

syntax

(define-unit/contract unit-id
  (import sig-spec-block ...)
  (export sig-spec-block ...)
  init-depends-decl
  optional-body-ctc
  unit-body-expr-or-defn
  ...)
 
sig-spec-block = (tagged-sig-spec [id contract] ...)
  | tagged-sig-spec
     
optional-body-ctc = 
  | #:invoke/contract contract
  | #:invoke/contract (values contract ...)
The define-unit/contract form defines a unit compatible with link inference whose imports and exports are contracted with a unit contract. The unit name is used for the positive blame of the contract.