Category:Smalltalk: Difference between revisions

Line 490:
 
==A Word About Code Snippets in Rosetta==
(a word to non-Smalltalkers which wantwanting to try the code)
 
Code snippets of Smalltalk code found in Rosetta are usually in the form of expression which can be copy-pasted into a so-called "''workspace''" (also called "''playground''" in other systems) which is a kind of REPL-like (read-eval-print-loop) evaluator. The details vary, but usually there is a tool window, into which code can be pasted and evaluated with a menu function called "doIt"or "printIt" (i.e. select the text and apply "doIt").
Line 507:
<br>a) find the Integer class in the class browser, enter the code and "accept" the code (which means: "compile and install the changes made").
<br>b) save the snippet to a file (in a fileIn format) and "fileIn" (aka. "load") the file.
 
In both cases, you'd end up with a system infected with many Rosetta methods, which you'd have to remove afterwards (using "undo" or "delete"). And because Smalltalk keeps track of your changes, it usually involves additional cleanup work in your change history.
With b) comes the additional trouble that fileIn formats are different (chunk file, vs. XML file, vs. Monticello, vs. GNU-ST etc.). So in which dialect's fileOut format should the example be presented to be most convenient?
 
Therefore, expression-like snippets work more or less in all dialects, and snippets are betterusually 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 |
Line 521 ⟶ 522:
 
The advantage is that this code can be simply selected as a whole and evaluated.
The disadvantage is that it looksmight look somewhat non-Smalltalk-like to not have it in a method.
 
==Citations==
Anonymous user