Expressions
Expressions are combinations of operators and operands which are evaluated to produce a value of a specific type. Infix operators require two operands with an operator written between them. A prefix operator requires one operand with an operator written before it. (The expression syntax starts on page 208.) Evaluation proceeds from left to right, governed by the precedence of the operators. The lowest numbered precedence as shown in Table 14.1 is evaluated first. Operators in the same row have the same precedence. Expressions enclosed by parentheses are evaluated before being treated as a single operand. An operand between two operators of different precedence is bound to the operator with the higher one; e.g., −10*20 means (−10)*20. An operand between two operators of the same precedence is bound to the one on the left; e.g., 10/20 * 30 means (10/20) * 30. Exercise 14.1 Work out the intermediate steps for this expression: … −2/(4+4)*5+6… When a null value is encountered in an expression where a non-null is expected, evaluation is short circuited and a null answer is produced. Otherwise, all expressions are fully evaluated even when the outcome is known after partial evaluation. Exercise 14.2 Can you think of an expression that does not require complete evaluation to get the correct answer? The operands of an operator must be compatible with the operator and with each other. Operands can be compatible without having identical types and are compatible when any of these conditions are satisfied: • The types are the same. • One type is a subtype of the other (e.g., one is a number and the other is an integer. • Both types are strings. • Both types are binaries. • Both types are arrays which have compatible base types and identical bounds. • Both types are bags which have compatible base types. • Both types are lists which have compatible base types. • Both types are sets which have compatible base types. Operations are organized by the kind of result they produce, namely: numeric, boolean or logical, string or binary, or aggregate.