Chapter 4
Program Control
Operators
Dylan provides a small number of unary and binary operators. These are syntactic shorthand for function calls or function-macro calls.
Because of the possibility of token blending, operators and
their operands must often be separated by whitespace. For
example, foo+bar
is a single named value reference, not an invocation of the
addition operator. The complete rules of tokenization are given in the lexical syntax
in Appendix A, BNF.
All binary operators are left-associative, except for the
assignment operator, :=
, and the exponentiation operator, ^
, which
are right-associative.
Each operator corresponds to a binding name, given in the table below. When an operator is called, the corresponding name is looked up in the environment of the call. (It is not looked up in the Dylan module, and will only refer to a binding in the Dylan module if that binding has been imported in the current module and has not been shadowed by a lexical binding.)
If the name given in the table has the same spelling as the operator, it must be escaped
with \ to be used as a named value reference. For example, to add
a method to +
with define method
, you use \+
. To
use <
as an argument to sort
, you write \<
.
The operands of a binary operator call that is shorthand for a
function call are executed in left to right order. The operands of a binary operator call
which is shorthand for a function macro call are passed to the macro. Their order of
execution depends on the definition of the macro. The built-in function macros are described
in Function Macros
on page 409.
The operators are listed in Table 4-1 in descending order of precedence. Operators within a group share the same precedence. When a function call using slot reference syntax appears as an operand, it has greater precedence than any of the binary operators.
Operator | Unary/Binary | Description | Name |
---|---|---|---|
- |
unary | arithmetic negation | negative |
~ |
unary | logical negation | ~ |
^ |
binary | exponentiation | ^ |
* |
binary | multiplication | * |
/ |
binary | division | / |
+ |
binary | addition | + |
- |
binary | subtraction | - |
= |
binary | equality | = |
== |
binary | identity | == |
~= |
binary | non-equality | ~= |
~== |
binary | non-identity | ~== |
< |
binary | less than | < |
> |
binary | greater than | > |
<= |
binary | less than or equals | <= |
>= |
binary | greater than or equals | >= |
& |
binary | logical and | & |
| |
binary | logical or | | |
:= |
binary | assignment | := |
Errata: In the published book,
the horizontal rule above :=
is missing, making it seem as though it has the
same precedence as &
and |
.