----- Original Message -----
From: "Rich" <
[hidden email]>
To: <
[hidden email]>
Sent: Monday, January 16, 2006 7:19 PM
Subject: Some newb questions
1. Suppose I were to execute the following block in the Workspace
|L|
L := (1 to: 5) collect: [:x | (x * x) + 2]
What would be the "scope" of L? Does workspace executed code get
wrapped into a no-parameter block which is then executed?
Not exactly. A code snipped that is evaluated is compiled as
method #DoIt in UndefinedObject. You can see that when you
evaluate:
| l |
self halt.
l := (1 to: 5) collect: [x | x * x + 2].
For compilation, L is made a temporary variable of the
method #DoIt. That is: The variable is part of the activation
record of that method and exists until that activation record
becomes eligible for garbage collection.
2. Is there a short-hand (syntactic) way to say "OrderedCollection
new", the way you can say #() to get a new, empty Array? Thank you
very much.
You can write
#() asOrderedCollection
but that is not really shorter than
OrderedCollection new.
Writing forms exist only for
* the logical values true and false
* the UndefinedObject (nil, this constant represents also what is called
nil pointer in some other languages)
* Integers and floats
* characters
* strings
* symbols
* arrays.
All other Objects have to be constructed. I should mention that
the class protocol of Collection has some convenient constructors
that simplify the task of object construction. Try for example
OrderedCollection with: (Array with: 'Hello' with: 123)
with: (Set with: 2 with: 15 with: 2)
Try also these two examples:
| a b |
a := #().
b := #().
a == b " two equal signs in sequence: identity check"
| a b |
a :=OrderedCollection new.
b := OrderedCollection new.
a == b "two equal signs in sequence: identity check "
The first example answers true (within one method, all
occurrences of the denotation #() are mapped to the same
empty array)
The second example answers false (Every call of new creates
a new instance. Therefore the identity check says no)
Boris