Perfect Developer basic tutorial 2 | This page last modified 2011-10-29 (JAC) |
We have already introduced some of the predefined operators for the built-in classes of Perfect. A full list of predefined operators and their preconditions may be found in the Library Reference chapter of the Language Reference Manual. The relative precedence of operators is more or less what most people expect, but if in doubt you can use brackets.
Unlike some programming languages, when you use a unary operator expression
as an operand of another expression, you need not use brackets around the
unary operator expression. So you will often see expressions like
<#accounts
. Can you
work out what this means, assuming that accounts is a seq of
something?
The comparison operators (including the equality operator) and their negated forms do not behave in quite the same way as other operators.
First, equality is defined automatically for all classes (although sometimes it is not available at run-time - see ghost methods). Also, you can compare any two expressions for equality; the only requirement is that the types of the two expressions overlap, i.e. they could conceivably be of the same actual type at run-time; otherwise they could never be equal.
The other comparison operators are all related to the binary ordering
operator ~~
, which may be pronounced compare. This operator returns a
value of the enumeration class rank whose members are below
,
same
and above
.
It is guaranteed that two equal values compare
same@rank
, also that
a ~~ b
yields the reverse
of b ~~ a
(i.e.
above@rank
swaps with
below@rank
). The compare
operator is also transitive. To see what this means, imagine that all the
possible values of a type are partitioned into a number of sets, and these sets
are placed in some linear order. Then to decide how two values rank with each
other, see which sets they belong to. If the first one belongs in a set that
comes before than the second, the answer is below@rank
; if they are in the
same set, the answer is same@rank
; otherwise it is above@rank
.
The binary operator >
is defined such that
a > b
yields true
when a ~~ b
yields
above@rank
; similarly
a < b
is equivalent to
a ~~ b = below@rank
. You
can also use a >~ b
which
yields true when a ~~ b
yields above@rank
or
same@rank
, or
a <~ b
which is
equivalent to a ~~ b ~=
above@rank
.
If the ~~
operator defines a total ordering (i.e. each partition contains
just one value), then two unequal values can never compare same@rank
. Under
these conditions, the comparison operators >=
and <=
are also defined to
mean the same as >~
and <~
and turn out to mean just what you would expect.
Unlike most programming languages, you can string comparisons together just
as in mathematics.
For example,
a < b < c
means the same as
a < b & b < c
.
The built-in classes int, char and bool have a
predefined operator ~~
which defines a total ordering. For
class int, the ordering is exactly what you would expect. For class char,
a ~~ b
is defined as (+a) ~~ (+b)
. For class bool, true
is considered greater
than false
. Operator ~~
is also predefined for all
enumeration classes.
Knowledge round-up quiz
Save My Place | Glossary | Language Reference Manual |
Copyright © 1997-2012 Escher Technologies Limited. All rights reserved. Information is subject to change without notice. |