The Protocol for Streams Introduction: Chapter 12 of the Blue Book introduces the Stream class. It seems advisable to show off some details of the "positional reference" that I overlooked whilst defining my first subclass of a stream. Another chapter of "Composition vs. Inheritance" might shed more light on streams. If you know Unix, be prepared for a flashback. (End of Introduction) The Positionial Reference. A stream lets you access the elements of a collection by a position reference, which resembles a Unix file, which lets you access its bytes by a pointer. But there is a subtle difference between a position and a pointer: The pointer addresses the n e x t byte to be accessed whereas according to the Blue Book the position addresses the l a s t object accessed. Question: What does "the last object accessed" mean after a new stream is created? Answer: Now and then the Blue Book poses the tasks "guess and try" on its readers. Since this is not one of my expertises, I consulted the V2 sources file. The method "ReadStream>>next" revealed to me that the position is incremented before it is used to access the next item of the associated collection. So the position refers to the object just before the next object to be accessed. This protocol wipes out a difference between Smalltalkers who regard one as the first natural number and aficionados of Unix who start counting at zero: The range of positions valid before the message ReadStream>>next is sent to a stream equals the range of pointers valid before the system call read() is invoked on a file, namely [0..size). That is, the valid positions range in have half open intervals starting at zero. Unfortunately this comes as a surprise in Smalltalk, since indexes of indexed instance variables start at one and usually intervals are specified including upper and lower bounds. Furthermore reading a file or stream comes to an end, if and only if the position respectively the pointer equals the size. (End of Answer) Streams and Collections. Question: With respect to functionality, streams and collections differ only in that streams have a position. In other words, a stream is a collection with a position. Why then did the programmer of streams not simply define a stream as a subclass of collection and extend it by access methods to the position? Answer: If a stream were a collection with a position, it would let you use one position at a time. On the contrary a stream has a collection and a position. This lets you use several positions for traversing one collection at the same by simply creating several streams on one collection. (End of Answer) Remark: Streams remind one of the file API in Unix. One file on disk, also known as inode, can be accessed by several opened files, also known as file descriptors. The pointer sticks with the file descriptor and not with the inode, just as the position sticks with the stream and not with the collection. (End of Remark) Remark: Streams and Collection serves as an example of "favor compostion over inheritance", an advice Josh Bloch gave in his "Effective Java". (End of Remark) (End of Streams and Collection) Remark: If the designer of streams knew Unix, he kept it as a secret. (End of Remark) (End of The Protocol for Streams) Date: 23.05.2006 Author: Wolfgang Helbig (helbigAtLehreDotBA-StuttgartDotDE)