This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: Extra quotes
Feed Title: David Buck - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/buck-rss.xml
Feed Description: Smalltalk can do that
I've been teaching Smalltalk since 1993 and one question I get frequently is "Why are there extra quotes in my stream?" I always have to explain that when writing to a stream, you have to use nextPut: to write characters, nextPutAll: to write strings, and print: to write other objects. On top of that, nextPutAll: is rather wordy to write. Your code starts to look like:
aStream
nextPutAll: 'Welcome to ';
print: city;
nextPut: $.
Why can't we define one method that just does the right thing (assuming there's typically one right thing). For example:
aStream
write: 'Welcome to ';
write: city;
write: $.
In Squeak, there is an implementation of write: that handles the string and character case but not the generic object case. Perhaps we should define it this way:
Stream
write: anObject
anObject putOn: self
String
putOn: aStream
aStream nextPutAll: self
Object
putOn: aStream
aStream print: self
Character
putOn: aStream
aStream nextPut: self
The other related problem is that printOn: produces a programmer-readable output, not something that's usable on a user interface. For UI's, though, you should really consider internationalization. In VW, you could do this by implementing putOn: for UserMessage.