Newcomer Trying to Learn Squeak

David T. Lewis lewis at mail.msen.com
Tue Dec 21 03:00:47 UTC 2004


On Mon, Dec 20, 2004 at 09:21:01PM -0500, Rob Rothwell wrote:
> Hello all,
> 
> I am brand new to Squeak and Smalltalk, and I am afraid I am
> misunderstanding a very important and probably straightforward concept:
> 
> How in the world can
> 
> fs := FileDirectory new fileNamed: 'c:\test.txt'.
> 
> Get turned into
> 
> fs := FileStream fileNamed: 'c:\test.txt'.
> 
> when fileNamed: is a method belonging to FileDirectory and not to
> FileStream?
> 
> Whatever makes this syntactically correct seems like a very useful concept
> that I can not seem to duplicate.
> 
> Thank you,
> 
> Rob Rothwell
> 

Before answering the question, let me suggest this. In a workspace, try
evaluating the following:

  self halt. FileStream fileNamed: 'test.txt'

This will cause a "Halt" dialogue box to come up. Click the "Debug"
button to get into a debugger, and start playing around with it.
You will be able to step through the code and see exactly what Squeak
is doing (well, it will take a few tries, but you'll get the idea,
and you can't hurt anything).

Back to your question: You are seeing the difference between sending
a message to a class, versus sending a message to an instance of
a class. Both a class and and instance of a class are just regular
objects, but they show up differently in the browsers, and it can be
a bit confusing figuring out which is which until you get the hang
of it.

When you do "FileDirectory new fileNamed: 'c:\test.txt' ", you are
sending the #fileNamed: message to an instance of class FileDirectory.
If you open a browser on class FileDirectory, and select the
"instance" button to look at the methods for an instance of the
class FileDirectory, you will find the #fileNamed: method as you
expected.

When you do "FileStream fileNamed: 'c:\test.txt\", you are sending
the #fileNamed: message to the actual class FileStream (not to
an instance of the class FileStream). The class is an object that
responds to messages like any other object, but if you open a
browser on the FileStream class, you need to click on the "class"
button to display the methods that are understood by this class.
Here you will find method called #fileNamed: which does something
very similar to the instance method in class FileDirectory.

HTH,
Dave




More information about the Squeak-dev mailing list