Smalltalk language limitation ?

David T. Lewis lewis at mail.msen.com
Sun Dec 5 17:50:12 UTC 2004


On Sun, Dec 05, 2004 at 05:20:31AM -0000, Aaron Gray wrote:
> >> If my understanding is right then you cannot send a message to an object 
> >> passed as a parameter to a method of another class.
> >
> > Why not show us a sample of what you mean.
> 
> Okay :-)
> 
>     class B
>         anotherMethod:
>             self doSomething:
> 
>     class A
>         aMethod: aParam
>             aParam anotherMethod:
> 
> That is probably not legal SmallTalk from what I understand on the VM ?
> 
> Or are method names unique ?

Hi Aaron,

Please try the following. It will take you about a half hour to do this
the first time, but I think you will be glad to have spent the time once
you have done it.

Make a new project:

 world menu
  -> open...
   -> morphic project

The new project will appear as a box with a label like "Unnamed2".
Click on the box to enter the project.

Open a browser and a workspace. Click on Tools flap (on the right side
of the screen). Use the mouse to drag a Browser onto your screen, then
do the same for a Workspace (you can also create these using the world
menu -> open...).

In your browser, experiment with your mouse buttons to see what they
do. On my computer (Linux), the left button selects things, middle button
brings up menus, and right button brings up the "Morphic halos" (little
buttons around the Morphs that make up your browser). On Windows,
the right button does the menu things, but I'll just refer to "middle button"
in what follows.

Make a new class category called "My Junk". In the upper left pane of
the browser (the one with the list of Kernel-Objects, Kernel-Classes, etc),
middle button click for menu, then "add item...". In the category name
prompt that comes up, type "My Junk", then click Accept.

Now in the large bottom pane of the browser, you can create your
new class. Where it now says "Object subclass: #NameOfSubclass", change
it to say "Object subclass: #MyTestClassB", then accept the text to create
the class. You can accept the text with keyboard <alt>s, or middle button
click and "select" from the menu.

MyTestClassB will show up in the second-from-left pane in the browser.
Underneath this, click the "?" button. The class comment entry area
appears in the bottom pane. Replace the text with a comment such as
"This is my test class B." Accept the text. (I'm just telling you this part
so you will be in the habit of doing it. Think of it as a note to yourself
explaining why you created the class, just in case you need to remember
it six months from now.)

Now click on the third-from-left pane, the one that says "no messages".
Do middle button click, then select "new category...". Select "new...", then
enter "my test methods" as your catagory. (Again, this is just to get in
the habit of putting things in categories that will mean something to you
six months from now).

Now you can enter a method. In the bottom pane, there is some example
text that suggests the layout of the method source code. Replace this with
your actual method, then accept it. Your actual method can look like this
(the underscore character will be a left-arrow when you type it into
Squeak):

anotherMethod: aParameter
	"This method does something with aParameter, but for now it will
	just halt so I can see it in a debugger."
	| myTempVar |
	myTempVar _ aParameter.
	self halt. "self doSomething might go here"
	^ myTempVar

Now you have your class B. You can give it a #doSomething:
method also if you want to have your #anotherMethod do a
"self doSomething: aParam"

Now make another class called MyTestClassB. Give it a method like
this:

aMethod: aParam
	"This method does something with aParam"

	| myFirstTempVar |
	myFirstTempVar _ aParam.
	self halt.
	^ myFirstTempVar

Now you can experiment and see how things really work. Go to your
workspace (the white window), and enter the following to get started:

anInstanceOfB _ MyTestClassB new.
anInstanceOfA _ MyTestClassA new.
anInstanceOfB anotherMethod: anInstanceOfA.

Accept this text in the workspace (<alt>s), then highlight it with the
mouse. Middle button click, and select "inspect it".

Your code will start running, and stop with a red "Halt" dialog when
it reaches a "self halt" in your code. Click the "Debug" button to go
into the debugger and see what is happening. (Note: You can also
run code in the debugger just by putting a "self halt"  in the text
on the workspace, right before the code you want to evaluate).

In the top pane of the debugger, you will see levels of the execution
stack. Click on the second from the top, where you see the
"myTestClassB>>anotherMethod:", and you will now see your
source code in the middle pane.

In the lower right debugger pane, enter the text
"myTempVar aMethod: self". Highlight your text, then middle button
click, and "Inspect" it. You will get another halt dialog to put
you into another debugger, which will show the result of sending
your instance of MyTestClassB as a parameter for the #aMethod
message sent to MyTestClassA.

Now it's time to start exploring and experimenting. I won't try to
explain how to use the debugger, just start clicking on things and
see what's what. You have experience with other languages, so
it will probably make sense to you right away. Smalltalk is a stack
oriented virtual machine, so it should look familiar if you have
worked with Java or anything similar. Just click on levels of the
stack (top debugger pane) and experiment to your heart's content
with the things in the other debugger panes. Try clicking on things,
double clicking on things, and middle button click to see menu options.

You can do *anything* you want right in the debugger, including
change your code and proceeding. You can inspect variables and
parameters, you can send messages to them, you can see exactly with
is "visible" at any point in the execution, and so forth. Try treating
the panes at the bottom as little interactive workspaces, and if you
want to modify any of the code you are experimenting with, do
it right in the big middle code pane.

Have fun,

Dave

p.s. One last thing that might interest you: Go back to your Browser
and click on one of your methods so you can see the source code.
In the middle of the browser is a row of buttons, click on the one
on the far right labelled "source". In the "What to show" dialog,
select "byteCodes". Now you are looking at the stuff that matters
to the virtual machine.




More information about the Squeak-dev mailing list