March 94 - Protocols
Protocols
Mikel Evins
What are protocols? A protocol is a specification for the interface of an object. Each
protocol consists of an abstract description of operations that can be performed on a
set of data objects. Less technically, a protocol describes the things that a given object
can do. It describes the operations that work on an object; it doesn't say anything about
how those operations are to be done, just what they are and what their interface is.
Let's consider an example from the Dylan programming language: the iteration
protocol. As described in the book Dylan: an object-oriented dynamic language [Apple
Computer, 1992], the Dylan programming language includes a set of collection classes
that store groups of objects. Examples of collections include arrays, strings, tables,
lists, and so on. Each of these classes has a different characteristic way of storing its
components, and a correspondingly different performance profile. On the other hand,
all of the collection classes have certain things in common. They all store sets of more
than one object, for example, and a programmer might reasonably expect to iterate
over any collection's contents. Dylan specifies the iteration protocol in order to
address this expectation: the interface specified by the iteration protocol can be used to
visit each object in a collection for the purpose of performing an operation on it, and
the protocol is valid for any collection object.
Dylan's iteration protocol specifies the following interface:
initial-state collection => state
next-state collection state => state
current-element collection state => element
copy-state collection state => state
Names in boldface type are the names of Dylan functions; names in plain type are the
names of parameter types; the symbol => denotes a return value.
This protocol is independent of the specific types of the parameters. We don't know,
and we don't need to know, the specific type or representation of a collection or its
associated state. So long as the protocol's functions are defined to operate consistently
and give correct results, the protocol works and the objects in question can be
considered collections. In fact, any object whatsoever that participates in the iteration
protocol can be thought of as a collection, regardless of whether it shares any