Category:Smalltalk: Difference between revisions

Content deleted Content added
Line 488:
 
All systems provide the full source code of all class libraries which can be modified and extended by users. The commercial systems will provide the runtime (VM) as binary only.
 
==A Word About Code Snippets==
(a word to non-Smalltalkers which want 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").
 
Due to differences in how methods are defined in classes when code is to be imported ("fileIn"or "load code"), these snippets are usually expressions which can be evaluated out of a class context, often in a functional style (using blocks).
 
For example, if asked to provide an example for a factorial function, a typical Smalltalk solution would be to define a method in the Integer class called "factorial", which might look like:
<lang smalltalk>factorial
self == 1 ifTrue:[^1].
^self * (self - 1) factorial</lang>
(here self would be the Integer receiver object, and "ˆ" will return a value from the message send).
To get the factorial value, we'd evaluate in a workspace:<lang>10 factorial</lang>.
 
However, to get this code to be executed in a concrete Smalltalk system, you'd have two ways to go:
<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?
 
Therefore, expression-like snippets work more or less in all dialects, and snippets are better presented in a functional or expression style, which works outside any class. Typically these define a function (here called "block") and then call it.
For the above, this might look like:
<lang smalltalk>
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).
 
The advantage is that this code can be simply selected as a whole and evaluated.
The disadvantage is that it looks somewhat non-Smalltalk-like to not have it in a method.
 
==Citations==