Category:Smalltalk: Difference between revisions

Content added Content deleted
Line 474: Line 474:
<lang smalltalk>(a > 0) ifYouAreTrueThenDo:[ 'positive' printCR ] ifNotThenDo:[ 'negative' printCR ]</lang>
<lang smalltalk>(a > 0) ifYouAreTrueThenDo:[ 'positive' printCR ] ifNotThenDo:[ 'negative' printCR ]</lang>
actually, because these two return the value of the block they evaluated, it can also be used for its value (in C, the ternary if expression), as in:
actually, because these two return the value of the block they evaluated, it can also be used for its value (in C, the ternary if expression), as in:
<lang smalltalk>( (a > 0) ifYouAreTrueThenDo:['positive'] ifNotThenDo:['negative'] ) printCR</lang>
<lang smalltalk>outcome := (a > 0) ifYouAreTrueThenDo:['positive'] ifNotThenDo:['negative'].
outcome printCR</lang>
Finally, by adding a self-returning "value" to the Object class, we can also write:
Finally, by adding a self-returning "value" method to the Object class, we can also write:
<lang smalltalk>( (a > 0) ifYouAreTrueThenDo:'positive' ifNotThenDo:'negative' ) printCR</lang>
<lang smalltalk>( (a > 0) ifYouAreTrueThenDo:'positive' ifNotThenDo:'negative' ) printCR</lang>
(knowing that the if methods send <tt>"value"</tt> to the corresponding arg and return that)
(knowing that the "ifXX"-methods send <tt>"value"</tt> to the corresponding arg and return that)


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 for extension, you can easily add additional convenient control functions (which is one place, where dialects differ, so very often, one has to transport some of those when porting apps from one dialect to another).
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 for extension, you can easily add additional convenient control functions (which is one place, where dialects differ, so very often, one has to transport some of those when porting apps from one dialect to another).