On Tue, 17 May 2011, Eliot Miranda wrote:
snip
I'm just trying to eliminate the explicit references to CrLfFileStream in the Cog VMMaker, but I've hit a snag. e.g. there's this idiom:
InterpreterPlugin class methods for translation storeString: s onFileNamed: fileName "Store the given string in a file of the given name."
| f | f := CrLfFileStream forceNewFileNamed: fileName. f nextPutAll: s. f close.
I prefer the *fileNamed:do: versions, because they give less work for the finalization process:
FileStream forceNewFileNamed: fileName do: [ :file | file nextPutAll: s ].
that I'd like to replace with
VMMaker class methods for utilities newFileStream "Always output files in unix lf format. A single format is friendlier to e.g. external version control systems. The Microsoft and old MacOS classic C compilers all accept lf format files."
^MultiByteFileStream new lineEndConvention: #lf; yourself
InterpreterPlugin class methods for translation storeString: s onFileNamed: fileName "Store the given string in a file of the given name."
| f | f := VMMaker newFileStream forceNewFileNamed: fileName. f nextPutAll: s. f close.
but only class-side methods understand forceNewFileNamed:, and hence one would have to write
InterpreterPlugin class methods for translation storeString: s onFileNamed: fileName "Store the given string in a file of the given name."
| f | f := MultiByteFileStream forceNewFileNamed: fileName. f lineEndConvention: #lf. f nextPutAll: s. f close
which is a tad tedious and distributed. I could have a set of convenience methods in VMMaker class, forceNewFileStream: oldFileStream etc. There could be subclasses of MultiByteFileStream purely for instance creation (MultiByteLfFileStream et al). Or...? Any thoughts?
Or pass everything to the method of VMMaker:
InterpreterPlugin class >> #storeString: s onFileNamed: fileName
VMMaker newFileWith: #forceNewFileNamed: named: fileName do: [ :file | file nextPutAll: s ].
VMMaker class >> #newFileWith: creatorSelector named: filename do: aBlock
| file | file := MultiByteFileStream perform: creatorSelector with: filename. ^[ file lineEndConvention: #lf. aBlock value: file ] ensure: [ file close ].
If you find #newFileWith:named:do: too long, then you can still add convenience methods which all send this one, e.g.:
VMMaker class >> #forceNewFileNamed: filename do: aBlock
^self newFileWith: #forceNewFileNamed: named: filename do: aBlock
Subclassing is also possible, but you can't override #initialize yet, because FileStream and subclasses don't use it (#new doesn't sent it). The changes I just pushed to the Inbox also fix this.
Levente
best, Eliot
snip