Category:Smalltalk: Difference between revisions

m
Line 390:
 
===Message Sends===
<lang smalltalk>
<lang smalltalk>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"
then send 'even' to whatever that returned"
 
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 , b "send the (binary) ',' message, which does collection-concatenation (arrays, strings, etc)"
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"
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"
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"
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... ]. "assign a block to the variable 'b'"
...
b value "evaluate the block's code (call the lambda closure)"
 
b2 value:123 "evaluate another block's code, passing anone argument"</lang>
 
=== Other ===
Anonymous user