Category:Smalltalk: Difference between revisions

Content added Content deleted
Line 390: Line 390:


===Message Sends===
===Message Sends===
<lang smalltalk>
<lang smalltalk>1000 factorial "send the 'factorial' message to the integer receiver"
1000 factorial "send the 'factorial' message to the integer receiver"


a factorial even "send the 'factorial' message to whatever "a" refers to, then send 'even' to whatever that returned"
a factorial even "send the 'factorial' message to whatever 'a' refers to,
then send 'even' to whatever that returned"


a + 1 "send a '+' message, passing the argument '1' to whatever a refers to"
a + 1 "send a '+' message, passing the argument '1' to whatever 'a' refers to"


(a + 1) squared "send '+' to a, then send 'squared' to whatever we get from it"
(a + 1) squared "send '+' to 'a', then send 'squared' to whatever we get from it"


a , b "send the (binary) ',' message, which does collection-concatenation (arrays, strings, etc)"
a , b "send the (binary) ',' message,
which performs collection-concatenation (arrays, strings, etc)"


arr at:1 put:'foo' "send the 'at:put:' message to 'arr', passing two arguments, the integer '1' and a string"
arr at:1 put:'foo' "send the 'at:put:' message to 'arr',
passing two arguments, the integer '1' and a string"


a > b ifTrue: [ a print ] "send the 'ifTrue:' message to whatever 'a > b' returned (a boolean, usually), passing a block closure as argument. The implementation of boolean will either evaluate or not evaluate the passed block's code"
a > b ifTrue: [ a print ] "send the 'ifTrue:' message to whatever 'a > b' returned (a boolean, usually),
passing a block closure as argument.
The implementation of boolean will either evaluate
or not evaluate the passed block's code"


a > b ifTrue: [ a ] ifFalse: [b] "send 'ifTrue:ifFalse:' to the object returned by 'a > b', passing two block closures as arguments. The 'ifTrue:ifFalse:' method will evaluate one of them and return that block's return value as its own return value"
a > b ifTrue: [ a ] ifFalse: [b] "send 'ifTrue:ifFalse:' to the object returned by 'a > b',
passing two block closures as arguments.
The 'ifTrue:ifFalse:' method will evaluate one of them
and return that block's return value as its own return value"


b := [ ... somCode... ].
b := [ ... somCode... ]. "assign a block to the variable 'b'"
...
...
b value "evaluate the block's code"
b value "evaluate the block's code (call the lambda closure)"


b2 value:123 "evaluate another block's code, passing an argument"</lang>
b2 value:123 "evaluate another block, passing one argument"</lang>


=== Other ===
=== Other ===