Version: 4.1

14.5 Whole-module Signatures and Units

In programs that use units, modules like "toy-factory-sig.ss" and "simple-factory-unit.ss" are common. The scheme/signature and scheme/unit module names can be used as languages to avoid much of the boilerplate module, signature, and unit declaration text.

For example, "toy-factory-sig.ss" can be written as

  #lang scheme/signature

  

  build-toys  ; (integer? -> (listof toy?))

  repaint     ; (toy? symbol? -> toy?)

  toy?        ; (any/c -> boolean?)

  toy-color   ; (toy? -> symbol?)

The signature toy-factory^ is automatically provided from the module, inferred from the filename "toy-factory-sig.ss" by replacing the "-sig.ss" suffix with ^.

Similarly, "simple-factory-unit.ss" module can be written

  #lang scheme/unit

  

  (require "toy-factory-sig.ss")

  

  (import)

  (export toy-factory^)

  

  (printf "Factory started.\n")

  

  (define-struct toy (color) #:transparent)

  

  (define (build-toys n)

    (for/list ([i (in-range n)])

      (make-toy 'blue)))

  

  (define (repaint t col)

    (make-toy col))

The unit simple-factory@ is automatically provided from the module, inferred from the filename "simple-factory-unit.ss" by replacing the "-unit.ss" suffix with @.