► |  | Guide: PLT Scheme

1 | Welcome to PLT Scheme
2 | Scheme Essentials
3 | Built-In Datatypes
4 | Expressions and Definitions
5 | Programmer-Defined Datatypes
6 | Modules
7 | Contracts
8 | Input and Output
9 | Regular Expressions
10 | Exceptions and Control
11 | Iterations and Comprehensions
12 | Pattern Matching
13 | Classes and Objects
14 | Units (Components)
15 | Reflection and Dynamic Evaluation
16 | Macros
17 | Performance
18 | Running and Creating Executables
19 | Compilation and Configuration
20 | More Libraries
21 | Dialects of Scheme
| Bibliography
| Index

▼ | 14 | Units (Components)

14.1 | Signatures and Units
14.2 | Invoking Units
14.3 | Linking Units
14.4 | First-Class Units
14.5 | Whole-module Signatures and Units
14.6 | unit versus module

Version: 4.1.4
top← prev up next →

#### 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 @.

top← prev up next →