LispWorks User Guide and Reference Manual > 20 User Defined Streams > 20.2 An illustrative example of user defined streams


20.2.3 Stream directionality

Streams can be defined for input only, output only, or both. In our example, the unicode-ls-stream class needs to be able to read from a file and write to a file, and we therefore defined it to inherit from an input and an output stream class. We could have defined disjoint classes instead, one inheriting from fundamental-character-input-stream and the other from fundamental-character-output-stream. This would have allowed us to rely on the default methods for the direction predicates.

However, given that we have defined one bi-directional stream class, we must define our own methods for the direction predicates. To allow this, the Common Lisp predicates input-stream-p and output-stream-p are implemented as generic functions.

(defmethod input-stream-p ((stream unicode-ls-stream))
  (input-stream-p (ls-stream-file-stream stream)))
(defmethod output-stream-p ((stream unicode-ls-stream))
  (output-stream-p (ls-stream-file-stream stream)))

The above code allows us to "trampoline" the correct direction predicate functionality from file-stream , using the ls-stream-file-stream accessor we defined previously.

LispWorks User Guide and Reference Manual - 22 Dec 2009