smalltalk - Subclassing Stream -
i interested in creating own stream subclass , i'm wondering methods should override (deploying on pharo , gemstone). have collection various types of things in , want able stream on subset of it, containing elements of class. don't want copy collection or use collect: block because collection may large. first use case this:
stream := self mailbox streamof: qturnmessage. stream size > 1 iftrue: [ ^ stream at: 2 ] iffalse: [ ^ nil ]
any pointers on methods override?
in smalltalk, when stream
refer objects respond basic protocol given few methods such #next, #nextput:, #contents, etc. so, before going further detail stream at: 2
, put in example, not appropriate expression. more appropriate expressions stream
be
stream position: 2. ^stream next
so, first thing have consider whether looking stream
or collection
. basic decision depends on behavior objects have implement.
use subclass of stream
in case decide want enumerate elements using #next, i.e. in sequential order. however, if want access elements via at: index
, model objects subclass of sequenceablecollection.
in case choose streams, have decide whether accessing them reading operations or want modify contents. description of problem seems indicate read them. therefore basic protocol have implement first is
#next "retrieve object @ following position , advance position" #atend "answer true if there no more objects left" #position "answer current position of implicit index" #position: "change implicit index new value"
also, if streams read only, make class subclass of readstream
.
there few other additional messages have implement if want inherit fancier methods. example #next:
retrives subcollection of several consecutive elements (its size being given argument.)
if instead think better model objects collections, basic protocol have implement consists of following 3 methods
#at: index "retrieve element @ given index" #size "retrieve total number of elements" #do: ablock "evaluate ablock every element of receiver"
(i don't think collections have support at:put:.
)
very had same problem describing , decided model our objects collections (rather streams.) however, regardless of approach follow think should try both , see 1 better. nobody give better advice smalltalk system.
incidentally, note if have (sequenceable) collection
, stream
free: send #readstream
collection!
Comments
Post a Comment