'Hello World' asUppercase asLowercase.
The String object 'Hello World' receives the message
asUppercase, which returns the new String. The next
message, asLowercase, is sent to this new String. To make this clearer,
we could use parentheses:
('Hello World' asUppercase) asLowercase.
Parentheses do not change the evaluation order;
they
only act as a stack boundary indicator. The following
code illustrates this:
5 + (3 * 3). Answers 14.
5 + 3 * 3. Answers 24.
The semi-colon allows the programmer to send multiple
messages to the same object, as in this example:
'Hello World' asUppercase;
beginsWith: 'Hello World'.
This example returns True since beginsWith:
was sent to
the original 'Hello World' String and not the uppercase
instance. Using the semi-colon enables cascading messages, which are
sent to the first message within a statement. This brings us to the
period, which behaves similar to how it does in English, - to terminate
a sentence. When Smalltalk encounters a period, it resets its internal
stack of object results. Periods are required at the end of every
statement until Smalltalk encounters a return (the carrot ^ selector),
where the period is optional.
When cascading messages it may be important to
return the original object on the stack and not the
result of the last message sent. To do this, send the cascaded message
yourself, such as in this example:
'Hello World' asUppercase;
beginsWith: 'Hello World';
yourself.
This will return 'Hello World' and not true as
might initially
be expected.
Blocks
The last syntactical element to discuss is the block, as in
this example:
|
True == True |
|
|
ifTrue: |
|
|
|
[Transcript |
|
|
|
|
show: 'Yes, true does |
|
|
|
|
|
equal true.'; |
|
|
|
|
cr] |
|
|
ifFalse: |
|
|
|
[Transcript |
|
|
|
|
show: 'No, true doesn't |
|
|
|
|
|
equal false.'; |
|
|
|
|
cr]. |
Blocks, which are real objects in Smalltalk, are
represented by the square brackets [ and ], one could write code like
this:
Future
Issue Topics
Data Mining, Modeling and
Simulation
Evolutionary Programming, Business Intelligence
Genetic Algorithms, Common Sense
Robotics, Pattern Matching
Intelligent Agents, AI Languages
Natural Language Processing
Web Based Expert Systems
|
|
|
|
aBlock : = [:a :b | a + b]. |
|
aBlock value: 10 value: 10. |
|
|
Answers 20. |
The actual class instantiated in these instances
is
BlockClosure. Blocks are a lexical closure, which defines its
own execution context. Blocks defines two input parameters, a and
b. It is theoretically possible to support an infinite number of input
parameters to a block, but most Smalltalks do have an upper limit.
In practice, four input parameters to a block is the maximum used.
One of the great aspects of Smalltalk is that it is easily to modify
BlockClosure.
Looping, conditional logic, collection iteration, threads,
and a variety of other common algorithms use blocks - truly one of
the best features in the Smalltalk language for leveraging development
time.
C++ programmers may see blocks as a type of function
pointer, but they are much more than this. Java has had inner-classes
since version 1.1. While not completely analogous to blocks, Java
inner-classes - specifically anonymous inner-classes - can emulate
Smalltalk blocks. However, Java inner-classes are much more verbose
and lack the level of control that Smalltalk blocks offer the programmer.
World Class Libraries
Although one of the main attractions of Java is its class
library, it does not compare to the maturity or sheer size and breadth
of functionality supplied by Smalltalk class libraries. Because Smalltalk
takes object-oriented design and implementation to its maximum extent,
the class libraries maximize code reuse, granting Smalltalk programmers
a high degree of flexibility.
While Java GUI frameworks like AWT (Abstract
Window Toolkit) and Swing have had a rocky life, Smalltalk's the originator
of the Model View Controller paradigm and building GUI applications
is a breeze. Both AWT and Swing have suffered from poor performance
while Smalltalk does not have those performance problems primarily
due to its maturity. While Java is playing catch-up, Smalltalk has
been performing for decades.
One concept that is difficult for programmers to
understand when reviewing Smalltalk is that a programmer can use multiple
Smalltalks. As we will see later, each product fits into a particular
niche and this fact can influence the choice of Smalltalk.
Common Usage Patterns
To better understand how much advantage Smalltalk
can give a programmer, we need to understand a couple in-depth examples.
The first is operating on a collection:
AllOrders select: [:each | each
backordered = true]
Here AllOrders represents a collection of order objects
and we select each that is flagged as a back-order. It is easy to
understand; here is the equivalent code example in Java:
|
List backordedList = (List) new |
|
|
ArrayList(); |
|
Iterator anIterator = |
|
|
Order.getAllOrders().iterator(); |
|
while( anIterator.hasNext() ) |
|
|
{ Order anOrder = (Order) |
|
|
|
anIterator.next(); |
|
|
|
if( anOrder.isBackOrdered() == |
|
|
|
|
true ) { |
|
|
|
|
|
backorderdList.add( |
|
|
|
|
|
anOrder ); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|