Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Smalltalk
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Smalltalk
(section)
Add topic