Please send questions to
st10@humboldt.edu .
An example involving super and self (and a little method syntax):
* here, when defining a method aMethod that does the same thing as the
superclass's version of aMethod, and then some
>>aMethod
super aMethod.
self doSomeMoreStuff.
* here, defining aMethod to do some stuff, and then follow it up
with whatever the superclass does:
>>aMethod
self doSomeStuff.
super aMethod.
Syntactic forms
* remember: in Smalltalk,
* everything is an object!
* almost all code takes the form
anObject withSomeMessageSentToIt
1. Unary message send
object isSentThisMessage.
13 negated.
'squeal' reversed.
2. Binary message send
object <isSentThisBinaryOperatorMessage> withThisObjectAsOperand.
3 + 2.
3. Keyword message send
object isSentThisKeywordMessage: withThisObjectAsArgument.
...in C, this might look like:
isSentThisKeywordMessage(object, withThisObjectAsArgument);
...in C++, this might look like:
object.isSentThisKeywordMessage(withThisObjectAsArgument);
object isSent: thisObject and: thisOtherObject.
(by the way: in Smalltalk parlance, the name of the message here
is said to be isSent:and:
...in C, this might look like:
isSentAnd(object, thisObject, thisOtherObject);
...in C++, this might look like:
object.isSentAnd(thisObject, thisOtherObject);
object is: sent this: message with: 4 arguments: ok.
(message here is is:this:with:arguments:)
(the 4 arguments: sent, message, 4, ok)
(but its arity is 5, counting the recipient object...!)
object is: sent this: message with: 4 arguments: (1+2).
...the 4th argument, here, is the object resulting from
the message + being sent to 1 with argument object 2
4. A block (a.k.a. closure, or block closure)
[thisObject willGetThisUnaryMessageSentToIt]
[:someObject | someObject willGetThisMessage]
[:first :second | thisObject gets: first and: second]
[:first :second | first gets: thisObject and: second]
it isn't EXACTLY true, BUT a block CAN be thought of
as the ONLY instance of an IMPROMPTU class with NO
superclass and ONE method...
* what IS that one method?
If a block has: then its only known method is:
---------------------------------------------------------------
no arguments value
["an argumentless block"]
one argument value: actualArgument
[:x | "a one argument block"]
two arguments value: firstActual value: secondActual
[:x :y | "a 2-argument block"]
* examples:
[object messageSent] value.
...when this block receives the unary value message,
then messageSent will be sent to object
[:one | "any code can be in here" ] value: object.
...the value: object message causes the parameter :one to
be bound with the argument object, and the "code" then
executes;
this can be lambda-like! consider:
fct := [:x|x + 2].
fct value: 3.
5. Answer (a.k.a. return a value)
^resultingObject
* EVERY method has at least one of these, even if you can't see it (!)
* Usually you CAN see it, and it is typically the last line of
the method --
but if you cannot see it, pretend you saw ^self as the
last line of the method
(that is, the object itself is returned)
* can use to "exit" a method early:
object isNil ifTrue: [^thisObject].
object getsThisMessage.
^self
6. Method definition
When using a [Smallltalk] browser, you don't actually see this
syntactic form, but you will when Smalltalk code is being given
outside of the Smalltalk environment...
Unary method:
ClassName>>methodSelector
someObject getsThisMessage.
someOtherObject getsThisOtherMessage.
^answerYetAnotherObject.
Binary method:
ClassName>>+ operand
instanceVariable := instanceVariable + operand.
^self
Keyword method:
ClassName>>keyword: object message: text
Transcript nextPut: object; nextPut: ' '; nextPut: text; cr.
This means class ClassName has a method definition for the
2-parameter keyword message keyword:message: and its definition
is as shown
7. Assignment
it has := -- for example,
b1 := 3 + 2.
* not considered a binary message;
* and its syntax is slightly different...
8. Cascade
has ;'s
send the following message to that SAME object that received
the preceding message
Transcript nextPut: object; nextPut: ' '; nextPut: text; cr.
above, the Transcript object gets all of those messages!
One more comment on precedence!
message precedence
unary highest
binary
keyword
assignment lowest
otherwise strictly left to right
[ ] whileTrue: [ ]
* send the keyword message whileTrue: (with its argument,
the second block) to the first block
* when the first block gets this message, it evaluates
itself (sends itself the value message)
* If the result is true, it sends a value message to
the second block, and then starts over;
* otherwise, it just quits, and answers false...