What are the point of contact of Smalltalk and other languages such as C and Java? Smalltalk is executed using a Virtual Machine (VM) as Java. The VM has a very complex Garbage Collector (GC) very similar to the HotSpot. And the Smalltalk code is stored in a byte-code form. But Smalltalk holds all the code in only one file (the image) and store in it all the state of the system (working Threads and so on). The image is coded in a binary-independent format (as java .class files) and can be ported across different platform. For instance, Squeak can run on Macintosh, Unix, OS/2, Ms-Dos, Windows 9x/NT/2000, and Windows CE. Even, it is easy to port the VM to other platform, because is coded in C language.
The Entire IDE, and the GUI are written in Smalltalk, mostly using the MVC (Model View Controller) design model.
Even the Java Swing uses a MVC-like model. Squeak has even a best and powerful GUI, called Morph. See the figure Morph.
The Smalltalk IDE uses reflection for exploring itself in a very massive way. For example, writing a Proxy object is trivial!!
Every class should have a comment: like in the Javadoc, you can integrate the documentation in the code! Squeak can create even hyper-link between two line of code or two comments!
About namespace: before the 1999, the concept of name-space wasn't implemented in the commercial versions of Smalltalk. For instance, in Squeak all the global objects are stored in a object called Smalltalk. VisualWorks 5 provide namespaces, and Squeak has a similar way for isolate projects code.
Exploring the big Squeak library is impossibile in this small tutorial, because of its size. But I'd like to show you only a small idea of some of the applications you can use:
Morphic: not only boring widgets!
With Squeak, and in less of 7Mb of image, you get a Email reader (Celeste), a Flash reader, a small Web browser (Scamper) a dynamic web server (PWS) and a special 3D library (3DBalloon) integrated with the Alice Authoring Tool http://www.alice.org. And even for music, you can play midi, digitalize sound and so on!
Note: The base Java 1.3 library is about 12Megabytes (uncompressed) without an ide or sound support!
The reason is simple: the .class format need to explicit say to the Java-VM what other classes it needs, for dynamic link. This is add a lot of redundancy. For instance, if you compress the rt.jar file, you get 4.3 Megabytes (ratio 65.6%). If we try to gzip the Squeak image, we got 3.10Mb (ratio 53.7%). So the Smalltalk image has less redundancy!
When you program in Java, you use types. You use int-s, floats, classes and so on.
We have seen a Class can be a nice place to put a struct. In Java, you often need to do a cast from a type to another. This is very common, and no one has trouble. In Smalltalk, the Virtual Machine only understands objects. If we execute 1+2 the VM of Smalltalk search the method + in the object 1, instance of the Number class. If Smalltalk do not found the method, will throw a Exception at Runtime
Exceptions are defined in ANSI Smalltalk. Some Smalltalk implementation may not have Exception, but can raise Errors.
The language-designers say type are userful, because the compiler can produce efficient code if it knows the type we are using (the sum of an two numbers is fast if they are integer and not float!).
Someone thinks type are userful to human-programmers, but in my own opinion, after two years of Smalltalk programming, it is not-so-true.
So, let's start to see how to program without type (and without casts...or other magic powers...)
This is a small overview of the base library of the Squeak 2.8.
First of all, suppose you have a unordered collection, and you want to eliminate duplicates. Doing this in Java is boring, in Smalltalk is quite easy:
|uc unq| uc:=OrderedCollection new. uc add: '2' ; add: '1'. uc add: '3'; add:'3'. unq:=uc asSet.
If print unq, you get:
Set ('3' 1 '2' )
Now you can sort it simply using:
SortedCollection ('1' '2' '3' )
The large base library of Smalltalk can manage in this way complex data structures.
If you want collect items, try this:
|c s| c:=OrderedCollection new. c add: 1 ; add: 1. c add: 2; add:3; add:4. s := c select: [:i| i <=2 ]. "[*] Collect items < 2" Transcript cr; show: (s asString) ; cr.
OrderedCollection (1 1 2 )
First, we create a collection with five elements (1,1,2,3,4). Then the collecion c is filtered to just the elements less than two (in line [*]). The Collection classes implements also a reject: message, which is the logial inverse of select:.
As you see, all in Smalltalk is written in Smalltalk! Not only the Smalltalk compiler is written in Smalltalk, but even the database holding the classes in the System is written in that language. A special dictionary, called Smalltalk, is a good starting point. Inspect it with:
The Smalltalk Dictionary contains a reference to ALL the class on the system. If you want know how much classes the system has simply print:
I have got 1193 classes on my Squeak 2.8 You can ask to the classes about their life and structure! Try for example:
(Smalltalk class) inheritsFrom: Object.
We ask if the class of Smalltalk (SystemDictionary) is a subclass (inheritsFrom...) of the class Object, and we get true as answer.
Now open a browser with:
and click on the '?' button for getting the comment of the class.