The last and most complex case uses named parameters,
where beginsWith: is both the name of the method and the colon signifies
that a parameter will follow. The parameter may be any object. Here
is another named parameter example:
aDictionary at: 'Bunny' put: 'Rabbit'.
Here the object aDictionary is sent the message
at:put: with each parameter inserted after the colon.
Classes and Variables
To extend and modify a Smalltalk image, add, remove,
or modify class objects. This is not the same as writing a class source-code
file in Java. In most Smalltalk products, a real object instance within
the image represents a class. While it is possible in most Smalltalks
to define a new class via Smalltalk code, it is rarely done. Instead,
the Class Browser, the Smalltalk equivalent to an IDE, creates the
classes and upon creation, it is alive immediately.
A class has two sides: class and instance. The closest
analogy with Java and C++ is the difference between statically and
non-statically scoped method functions or variables. However, this
is not a completely accurate comparison because Smalltalk treats classes
as any other object. Therefore, when you inherit from another class
within an image, both sides of the class are inherited, not just the
instance side. Both sides of a class support variables, which are
private to that side of the class.
Keeping variables in their own dictionaries prevents name
clashing between method names and variable names. It is common to
see getters and setters without the explicit get and set prefixes.
Many Smalltalks support the concept of a Shared
Variable or Pool Variable. These named aliases in the main symbol
dictionary can arbitrarily refer to any object instance (including
class instances) within a Smalltalk image.
PC AI Reprints
A Great Marketing Tool
|
Promote your company
with an excellent article in a targeted publication such as
PC AI.
Our staff will work with
you to re-layout your articles into full-color, customized
reprints. (Adding promotional material, technical details,
contact information and more).
Reprints are great as marketing tools,
trade-show handouts and in media kits.
For reprint price, quotes,
and policy
information call:
602.971.1869
|
|
|
Only one instance of any classes' class is instantiated,
making it act similar to a Singleton. Memory is the only limit to
instance side instantiation.
An interesting difference between Smalltalk and
Java/C++ is that the class side is responsible for instantiating the
instance side of a class. Therefore, Smalltalk has no official constructor
syntax similar to Java or C++. The Named Constructor Idiom is the
closest equivalent in the Java and C++ world to the way a Smalltalk
programmer instantiates a class instance.
In practice, Smalltalk's classes have so few restrictions
that they are much more powerful and robust than their Java or C++
counterparts. Since Smalltalk is dynamically typed, it is easy to
treat the class hierarchy as a type of super-schema, modified in real-time.
While most Smalltalk's do not directly support multiple inheritance,
many do support adding the functionality if desired. Java does not
support multiple implementation inheritance, and multiple interface
inheritance has many drawbacks.
Since there is no arbitrary limits on implementation
inheritance within Small-talk, as there is
in Java, so it is possible to change String or inherit from it.
Other Syntax
The remaining set of syntactical notations follows. The
first is variable assignment:
firstName := 'Hello World'.
Note that most Smalltalks implements variables as a
Dictionary with named aliases to their r-value. It is possible to
define context local variables by using the pipe symbol, known as
fence posts in Smalltalk. Here is an example:
| firstName lastName age weight |
firstName := 'Jeff'.
lastName := 'Panici'.
age := 127. "Feels that way sometimes"
weight := 175. "Well, that's what I
should weigh"
These variables persist through the lifetime of a context.
A context in Smalltalk is either a method or a BlockClosure, covered
later in the article.
To return a value from a method, code something like
this:
" Seems really old
"
^30
The words in double-quotes are comments and the
carrot tells Smalltalk to return the object following it. Although
some say this breaks the object message pattern, it really does not:
" Seems really old
"
thisContext ^ 30
This is the exact same code as the previous example;
with the implicit thisContext in the code, although most Smalltalks
do not require the explicit inclusion.
Parentheses serve a different purpose in Smalltalk than
most other languages. They act as a type of stack, where each object
returned from a previous send receives the next message sent, as in
this example: |