[Newbies] Re: Re: perform withArguments

Louis LaBrunda Lou at Keystone-Software.com
Tue Apr 24 14:49:04 UTC 2012


Hi Dawson and Dawson's 15 year old son,

>From your code below, I assume that somewhere in your program you have
created (instantiated) nine cells in instance variables named cell1-cell9.
This is fine but you should realize that they (cell1-cell9) are variables
that hold pointers to the objects and not really the objects themselves. It
is okay to think of them as the objects because for the most part it is
easier than thinking of them as pointers to objects.  The real point is to
realize that more than one variable can point to the same instance of an
object.  For example, if I were to write:

cell10 := cell1.

There would not be two copies of cell1, there would just be two pointers to
it.  Any messages sent to cell10 would effect cell1.  This can be
confusing, so I'm not recommending doing it exactly.  But putting the nine
cells in a collection is a good idea because it gives you another way to
get to the cells without using the original variables they were created in.

You ask about everything being an object is Smalltalk.  Yes everything is
an object.  You also ask if there is a way to construct the message with
the original variable names.  I don't know how to do it and don't think you
can with many Smalltalks.  Bert may know how to do it with Squeak.  Even if
Bert knows how, I wouldn't recommend it and I hope I'm not speaking out of
turn but I doubt Bert would recommend it either.  If it is doable, it's use
would be unusual and therefor confusing.  Even #perform: is seldom used by
most programmers and even less so by beginners.

Back to your program.  I assume that you have a good reason for putting the
cells in variables named (cell1-cell9) and again that is fine but there is
no reason why you can't have the same objects addressable or accessible
from a collection.

Smalltalk's collection classes are one of its many unknown treasures.  They
are a big part of why Smalltalk code is shorter and more understandable
than code of other languages.  Check them out.  And please keep asking
questions.

Lou

>Hi,
>actually, that is not quite what we want to do (we meaning my son and I
>in our first squeak project).
>
>Since we were unable to assemble the message, we ended up doing a
>brute force method to get it to work:
>
>lockCell: aBoolean row: aRow column: aColumn
>
>	self addressToCellNumber: aRow and: aColumn
>	"this converts the big grid aRow, aCoulumn into a cellNumber"
>	
>	(aBoolean isKindof: Boolean)
>	  ifTrue: [
>		   (cellNumber =1) ifTrue: [cell1 cellLock:aBoolean].
>		   (cellNumber =2) ifTrue: [cell2 cellLock:aBoolean].
>		   (cellNumber =3) ifTrue: [cell3 cellLock:aBoolean].
>		   (cellNumber =4) ifTrue: [cell4 cellLock:aBoolean].
>		   (cellNumber =5) ifTrue: [cell5 cellLock:aBoolean].
>		   (cellNumber =6) ifTrue: [cell6 cellLock:aBoolean].
>		   (cellNumber =7) ifTrue: [cell7 cellLock:aBoolean].
>		   (cellNumber =8) ifTrue: [cell8 cellLock:aBoolean].
>		   (cellNumber =9) ifTrue: [cell9 cellLock:aBoolean].
>		].
>
>As you can see that is really repetitive. So, it seemed to us that one
>should be able to dynamically build the message in one line,
>
>that is why we tried to do something like:
>
> self perform: ('cell', cellNumber asString) asSymbol ... to build up
>the "cell1 ... cell9" object. But we couldn't figure out how to build up
>the rest of the message.
>
>(One solution which is possible was presented by Louis Labranda which
>involves using an ordered collection ... that should work fine ... but
>is there no way of building up the message similar to our attempt, but
>expanding on it?  We thought that everything's an object in Squeak ...
>what happened to that?)
>
>Thanks,
>
>Dawson (I'm the under 15 year old's father, and I don't mind my name
>being published)
>
>
>
>
>
>
>On 24/04/12 7:59 AM, Bert Freudenberg wrote:
>> So if I understand correctly you want to do something equivalent to this:
>> 
>> 	cell1 cellLock: true.
>> 	cell2 cellLock: true.
>> 	cell3 cellLock: true.
>> 	... etc ...
>> 
>> Yes? If so, then perform is not what you are looking for. Perform lets you assemble the message selector, but not the receiver of the message. The Right Way to do this would be to have an Array of cells. Arrays are a collection of objects, and individual objects can be accessed by index. So if "cells" was an array of your cell objects, you could write
>> 
>> 	(cells at: 1) cellLock: true.
>> 	(cells at: 2) cellLock: true.
>> 	(cells at: 3) cellLock: true.
>> 	... etc ...
>> 	
>> but also iterate over all of them:
>> 
>> 	1 to: 10 do: [:i | (cells at: i) cellLock: true]
>> 
>> Does that make sense?
>> 
>> - Bert -
-----------------------------------------------------------
Louis LaBrunda
Keystone Software Corp.
SkypeMe callto://PhotonDemon
mailto:Lou at Keystone-Software.com http://www.Keystone-Software.com



More information about the Beginners mailing list