Anonymous user
Category:Smalltalk: Difference between revisions
→Control Structures
Line 394:
=== Control Structures ===
As mentioned above, these are defined as messages and their implementation is found in the corresponding receiver classes.
<br>Then, in the True class, define a method:
<lang smalltalk>True
ifYouAreTrueThenDo: arg1 ifNotThenDo: arg2
^ arg1 value
False
ifYouAreTrueThenDo: arg1 ifNotThenDo: arg2
^ arg2 value
Now, we can send this message to a boolean, and pass the code to be executed conditionally as a lambda block:
<lang smalltalk>(a > 0) ifYouAreTrueThenDo:[ 'positive'printCR ] ifNotThenDo:[ 'negative' printCR ]</lang>
In this style, all of Smalltalk's control structures, loops, enumeration, stream readers and event the exception handling constructs are built. And since every class is open, you can easily add additional convenient control functions.
The following is only a tiny subset - there are virtually hundreds or thousands of uses of blocks for control structures in the system.
Typical are:
<lang smalltalk>boolean ifTrue: [ block providing value if boolean is true ]
|