Category:Smalltalk: Difference between revisions

Content deleted Content added
Line 513:
Therefore, expression-like snippets work more or less in all dialects, and snippets are usually presented in a functional or expression style, which works outside any class. Typically these define a function (here called "block") and then call it.
<br>For the above, this might look like:
<lang smalltalk>factorial := [:n |
factorial := [:n |
n == 1 ifTrue:[ n ]
n * (factorial value:(n - 1))
].
factorial value:10</lang>
(here there is no self, and the code works anywhere outside a class context. the <tt>value></tt> message sent to the block called the function. There is no "ˆ" return statement, because the value returned from the block is the value of the last expression in it, and there actually will be no method from which to return).
 
The advantage is that this code can be simply selected as a whole and evaluated.