In addition to the equality and comparison operators, ScriptX also defines a full set of object comparison functions that can be used in a more general way than many of the operators. These functions are based on four generic functions, which can be redefined in new classes and objects. ScriptX equality and comparison functions are described in Chapter 3, "Working with Objects," and in the "Global Functions" chapter of the ScriptX Class Reference.The Comparison protocol is described in the "Object System Kernel" chapter of the ScriptX Components Guide.
Equality and Identity
==
and !==
), and three for
testing equality ( =
, !=
, and
<>
). All five operators return either
true
or false
.
==
|
tests whether two references are identical
|
!==
|
tests whether the references are not identical (the negation
of == )
|
The following examples illustrate the use of the identity
operators:
num1 := 3.14159265 -- a Float object
num2 := num1
num2 == num1
true
num1 !== num2
false
rect1 := new Rect x2:50 y2:50
rect2 := new Rect x2:50 y2:50
rect1 == rect2
false
num1
and
num2
are the same object in memory. In the third
test, although the Rect
objects in
rect1
and rect2
were defined in the
same way, and appear to be the same, they are different
objects in memory, so the identity test returns
false
. (Chapter 3,
"Working with Objects," describes how to define objects
using the new
method.)
Two
ImmediateFloat
or
ImmediateInteger
objects that have the
same value always appear to be the same object in identity
tests. For more
information on immediate objects, see the "Numerics" chapter
in the ScriptX
Components Guide.
=
|
tests whether two objects are equal
|
!=, <>
|
tests whether the two objects are not equal (the negation of
= ).
|
|
The not-equal operators are equivalent and can be used
|
|
interchangeably
|
The following examples illustrate the use of equality
operators:
num1 := 3.14159265 -- a Float object
num2 := num1
num2 = num1
true
rect1 := new Rect x2:50 y2:50
rect2 := new Rect x2:50 y2:50
rect1 = rect2
In the second example, the objects are different objects in
memory (the identity test evaluated to true
false
),
but they have been defined the same way with the same
parameters. Their values are the same, so the equality test
returns true
.num1 = rect1
false
Rect
objects
defined above might be the boundary for another ScriptX
object, such as a TextPresenter
object. These
TextPresenter
objects, in turn, might be embedded
in PushButton
objects.
Ordering
<
|
less than
|
---|---|
>
|
greater than
|
<=
|
less than or equal to
|
>=
|
greater than or equal to
|
Magnitude operators only work if the operands they are given
are comparable-that is, from the same or similar classes. If
you try to compare objects from classes that are not
comparable, ScriptX reports an error. Every object responds to
the
isComparable
method, a method defined by
RootObject
and inherited by all classes in the
system. The following test shows that num1
and
rect1
, defined in the previous section, are not
comparable.isComparable num1 rect1
false
isComparable
method. If a class
is comparable with another, then it must determine how the
comparison is made. For information on the Comparison
protocol, see the "Object System Kernel" chapter of
ScriptX Components Guide.true
or false
, as shown in the
following examples:3 < 4
true
3 <= 3
true
"apple" < "zucchini"
true
Equivalent Functions
2 + 2
into the generic
function call sum 2 2
. The sum
method is defined by the Number
classes, allowing
this expression to be evaluated correctly by any numbers.sum 2 2
4
equal "asparagus" "celery"
false
eq
(the equivalent function for ==
) and
ne
(the equivalent function for
!==
), which are global rather than generic. Note
that this means that eq
and ne
cannot be specialized, whereas the generic functions can. For
information on defining functions, see Chapter 5, "Functions, Threads and
Pipes." For information on creating new classes and
objects that use these functions, see ."
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.