Category:Smalltalk: Difference between revisions

Line 496:
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 the following naïve version:
<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>.
 
Anonymous user