Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Smalltalk
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Code blocks=== A block of code (an anonymous function) can be expressed as a literal value (which is an object, since all values are objects). This is achieved with square brackets: <syntaxhighlight lang="smalltalk">[ :params | <message-expressions> ]</syntaxhighlight> Where '':params'' is the list of parameters the code can take. This means that the Smalltalk code: <syntaxhighlight lang="smalltalk">[:x | x + 1]</syntaxhighlight> can be understood as: :<math>f(x) = x + 1</math> or expressed in lambda terms as: :<math>\lambda x.x + 1</math> and <syntaxhighlight lang="smalltalk">[:x | x + 1] value: 3</syntaxhighlight> can be evaluated as :<math>f(3) = 3 + 1</math> Or in lambda terms as: :<math>(\lambda x. x + 1)\,3 \underset{\beta}\rightarrow 3+1</math> The resulting block object can form a [[closure (computer programming)|closure]]: it can access the variables of its enclosing lexical scopes at any time. Blocks are [[first-class object]]s. Blocks can be executed by sending them the ''value'' message. Compound variations exist to provide parameters to the block e.g., <code>value:value:</code> and <code>valueWithArguments:</code>. The literal representation of blocks was an innovation which on the one hand allowed certain code to be significantly more readable; it allowed algorithms involving iteration to be coded in a clear and concise way. Code that would typically be written with loops in some languages can be written concisely in Smalltalk using blocks, sometimes in a single line. But more importantly blocks allow control structure to be expressed using messages and [[Polymorphism (computer science)|polymorphism]], since blocks defer computation and polymorphism can be used to select alternatives. So if-then-else in Smalltalk is written and implemented as <syntaxhighlight lang="smalltalk">expr ifTrue: [statements to evaluate if expr] ifFalse: [statements to evaluate if not expr]</syntaxhighlight> ''True methods for evaluation'' {{pre|'''ifTrue:''' trueAlternativeBlock '''ifFalse:''' falseAlternativeBlock<br /> ^trueAlternativeBlock value}} ''False methods for evaluation'' {{pre|'''ifTrue:''' trueAlternativeBlock '''ifFalse:''' falseAlternativeBlock<br /> ^falseAlternativeBlock value}} <syntaxhighlight lang="smalltalk">positiveAmounts := allAmounts select: [:anAmount | anAmount isPositive]</syntaxhighlight> This is related to [[functional programming]], wherein patterns of computation (here selection) are [[Abstraction (computer science)|abstracted]] into [[higher-order function]]s. For example, the message ''select:'' on a Collection is equivalent to the higher-order function [[Filter (higher-order function)|filter]] on an appropriate [[function object|functor]].<ref>{{cite book|last1=Goldberg|first1=Adele|author-link1=Adele Goldberg (computer scientist)|last2=Robson|first2=David|year=1989|title=Smalltalk-80 The Language|publisher=Addison Wesley|isbn=0-201-13688-0|pages=17β37}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)