Beyond Java - Bruce Tate [80]
1 to: 5 do: [:i | Transcript show: i]
First, you see that [ and ] mark the beginning and end of the code block. i is a parameter for the code block. In the declaration, you'll precede it with a colon.
Let's try multiple statements. Smalltalk terminates statements with a period. Logic uses messages and code blocks :
age := 4.
(age > 16)
ifFalse: [Transcript show: 'Youngster.']
ifTrue: [Transcript show: 'Old timer.']
This bit of code sets age to 4 with the := message. Then, it sends the ifFalse: method to the (age > 16) expression. The first code block is a parameter for ifFalse, and gets called if the expression evaluates to false.
You can see the influence of the elegance of Smalltalk in Java, and other languages, too. Java's garbage collection, design patterns, and collections all share Smalltalk's influence. Consider Hibernate's use of message chaining . If a method doesn't have a return value, it simply returns itself, enabling tighter code like this:
cfg.add("pet.hbm")
.add("vet.hbm")
.add("pet.hbm");
Many ideas from Eclipse have roots in IBM's VisualAge for Java , which first shared IDE code and a virtual machine with a Smalltalk product. Smalltalk syntax is wonderfully consistent.
A Seaside Overview
Seaside is a Smalltalk framework and a server. Remember, a continuation server is different from other web servers, so Seaside must run in its own environment. In Squeak, you'll left-click on the desktop to give you a menu (called the world menu). Then, you'll select Open... → SqueakMap Package Loader. Use it to install four packages: DynamicBindings, KomServices, KomHttpServer, and Seaside, in that order. Now, your Smalltalk image has Seaside. To see it, fire up the server. In Squeak, you'll open a workspace and evaluate:
WAKom startOn: 9090
WAKom is the name of the server. starton: is a method that tells the server to start on a supplied port, 9090 in this case. In some ways, WAKom is like Tomcat, or any other web application server. You can configure it by pointing your browser to:
http://localhost:9090/seaside/config
You'll see Seaside's configuration screen. Some of the items should look familiar to you. You'll see a list of registered applications, and some configuration options. Later, it will become clear that Seaside is more than Tomcat in Java.
A Seaside Example
Under the /seaside heading, notice the list of apps. One of the examples that you see in the configuration screen is store. Click on it. You'll see SushiNet , one of the more bizarre examples for web frameworks. In the search window, type the word Tuna. Click on two different tunas to add them to your cart. Now click the Back button and notice that you go back to a previous page, just the way it was. Add another tuna to your cart, and you'll notice that the old tuna item is still in your cart. So, you can override the Back button behavior, as needed.
Components
Notice the three boxes across the top of the screen, in Figure 8-3. Seaside is a component-based architecture . Each component has independent rendering, and each has a model behind it.
Figure 8-3. This Seaside application has three major components, each with independent rendering and business logic
This component-oriented approach often makes it much easier to design and refactor complex web screens. For example, here's the rendering for the shopping cart:
html divNamed: 'cart' with: [
html small: [html bold: 'Your cart:'].
html table: [
cart countsAndItems do:
[:assoc | self renderRowForCount:
assoc key of: assoc value on: html ].
html spacerRow.
html
tableRowWith: ''
with: ''
with: [html bold: cart totalPrice printStringAsCents].
]
Notice that Seaside components have code that generates HTML. Java people don't tend to like this approach either, but it's very productive in Seaside. The code in bold generates the table. First, you see the table message passed to the html object. This will generate table tags around the code block. Next, you'll see a loop that processes