[contents]
[prev] [next] [top] [bottom] (8 out of 17)
Assignment
To assign a value to a variable, use the assignment operator (
:=
).
location := expression
On the lefthand side, location is a variable name or
declaration. Most expressions in the ScriptX language can be
used on the righthand side. For more information on the
assignment expression, see the discussion on page 241 in Appendix A, "ScriptX Reference."
local var := 15
x := "string that goes in x"
Variables can be assigned to a value without being declared
first, as in the previous example. A warning is printed if you
do so. Variables that are assigned a value before they are
declared are automatically declared as global
variables, regardless of the scope in which they are used.
Variable assignment expressions evaluate to the value
assigned; this allows you to nest or cascade assignments
within other expressions:
z := 14 + (y := 3 - (x := 6))
11
In this example, the expression x := 6
evaluates
to 6
, and y
evaluates to
-3
, allowing the value 11
to be
assigned to z
.
Variable assignment in ScriptX is reference assignment.
Assigning one variable to another does not copy the object
that variable contains. Both variables contain a reference to
the same object. Consider the following assignment
expressions:
myRect := new Rect x2:100 y2:100
[0, 0, 100, 100] as Rect
myOtherRect := myRect
[0, 0, 100, 100] as Rect
In the first assignment, a new instance of the
Rect
class is assigned to myRect
(the new
method is described in the next
chapter). In the second assignment, myRect
is
then assigned to myOtherRect
, which has the
effect of setting myOtherRect
to reference the
same Rect
object that myRect
references.

Figure 2-2: Pointer assignment
If you change any properties of myRect
, you also
change myOtherRect
, since they both refer to the
same object in memory.
myRect.y2 := 60
60
myOtherRect
[0, 0, 100, 60] as Rect
Variables and the Garbage Collector
As a ScriptX program runs, many objects are created. When
there are no longer any references to those objects, the
garbage collector automatically removes them from memory and
reclaims the space they were using. This happens when all
variables that reference an object have other values assigned
to them, and there are no other references, such as by
membership in an array or through another object's instance
variables.
Each variable declaration uses only one word of memory, four
bytes on a 32-bit processor, until an object is assigned to
it. Objects are whatever size they are when instantiated. Once
an object is assigned to a variable, ScriptX keeps that object
in memory until there is no longer an active reference to it,
including any other variables that reference that object. Of
course, an object may itself contain references to other
objects. Some objects, such as arrays, contain references to
objects that are their members or elements.
Memory assigned to a local variable can be reclaimed at the
end of the scope in which that variable is declared. Memory
assigned to a global variable cannot be recovered unless the
value of the variable is set to undefined
, or the
variable is unbound.
To allow the garbage collector to reclaim memory from unused
objects, use local variables whenever possible. For global
variables, set the value of a variable that references the
object to undefined
(or some other value) when
the object is no longer needed. This allows memory to be
reclaimed without undoing the declaration itself.
global garbage := "taking up space"
"taking up space"
garbage := undefined
undefined
garbage
undefined
In this example, garbage
still exists as an empty
slot in memory. This slot points to the undefined
object, a system object.
This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.
Copyright 1996 Apple Computer, Inc. All Rights Reserved.