Declarative Objectivity (DO) Language : Operator Expressions
Operator Expressions
Operator expressions are expressions that use operators to test, transform, combine, or otherwise process input values (operands) to produce output values.
You combine an operator and its operands using a particular operator-expression format. When you combine multiple operators in nested expressions, DO evaluates them according to their precedence order. (As a convenience, you can verify the result of a particularly complex operator expression by performing a preliminary check with a SHOW statement.)
DO supports various categories of operators.
Note:You can check for more operators in a particular category by running the ListOperators tool in a command prompt—for example:
objy ListOperators -category Arithmetic  
 
Category
For Operators That Perform These Actions
Includes These Operators
Perform calculations on values obtained from a collection, sequence, or group of objects
AVG    COLLECT    MAX    MIN    SUM
 
COUNT
Perform arithmetic calculations (plus, minus, and so on) on numeric operands.
+    -    *    /    %  
 
IS_INF    IS_NAN
Perform bit-level operations on operands that are bit patterns or binary numerals.
&    |    ^    ~   
 
>>    <<
Test or count the elements of a collection.
COUNT    IS_EMPTY
 
ANY    ALL    OF 
 
ANY_EQUAL    IN   ALL_EQUAL   OF_EQUAL
Return information about operands that are dates, times, or datetime values.
DAY_NAME    DAY_OF_MONTH    DAY_OF_WEEK
 
MONTH    MONTH_NAME
 
WEEK    YEAR  
Perform logical operations (NOT, AND, OR, and XOR) on Boolean expressions, and return the result as a Boolean value.
!    &&    ||    ^^
Perform numeric, trigonometric, or logarithmic calculations on numeric operands.
ABS    RAND    SIGN   
 
CEILING    FLOOR   ROUND    TRUNCATE
 
ACOS    ASIN    ATAN    ATAN2    COS    SIN    TAN    PI
 
EXP    LOG    SQRT
Test a reference to an existing persistent object, or return the referenced object.
IS_NULL    IS_VALID
OBJECT
 
Compare two operands of the same type, and return a Boolean value indicating how they compare to each other.
==    !=   
 
<    <=    >    >=
Return (select) the value of reachable attributes or collection elements.
.    []
 
String Operators
(includes regular expression operators)
Transform or return information about a string value.
Compare a string value to a regular expression pattern, and return a Boolean value indicating whether the string matches the pattern.
CONTAINS    SUBSTR    UPPER    LOWER
 
=~    !~    =~~    !~~  
 
Provide access to objects based on their class.
CLASS_TYPE    KIND_OF    ELEMENTS_OF_TYPE
 
AS_TYPE    ELEMENTS_AS_TYPE
Note:Examples in operator-expression topics are based on the Rental Fleet Sample Schema.
About Operator Expressions
An operator expression is a specific type of expression that uses an operator to process some number of input values called operands, and produces a return value, which may be a sequence of multiple values.
Components of an Operator Expression
The tokens that represent DO operators can be symbols (such as ==) or names (such as COUNT).
Some operators have only a single token. For example, you must use the token >= to specify the greater-than-or-equal-to operator.
Some operators have multiple token synonyms. The choice depends on your stylistic preference. For example, you can use either the symbol && or the name AND to specify the logical conjunction operator.
Every operator defines the number of operands it can have.
Unary operators accept a single operand.
Binary operators accept two operands.
Several DO operators accept three or more operands.
Every operator defines the types of the operands it accepts, as well as the type of the value it returns. You specify operands using expressions. Any kind of expression is accepted—an attribute expression, literal expression, object structure, collection structure, interval structure, system value, and even another operator expression—as long as it evaluates to a value of the required type.
Some operators are overloaded, so their functionality depends on the types of operands you supply. For example, the PLUS operator can be:
Used with two numeric values to return another numeric value:
3 + 4 
Used with a date value and an interval to produce another date value:
2017-05-08 + INTERVAL{DAYS: 5}
Format of an Operator Expression
DO supports three formats for operator expressions:
Format
Description
Examples
Prefix
Operator is a prefix to the operand:
 
OPERATORoperand
!retired
Binary
Operator occurs between two operands, separated by spaces:
 
operand1 OPERATOR operand2
name == 'Acme'
license =~ '.*997$'
Functional
Operator precedes the operands, which are enclosed in parentheses and separated by commas:
 
OPERATOR(operand1, operand2, operand3...)
NOT(retired)
UPPER(brand)
ANY(vehicles, retired == true)
The format you use for a DO operator depends on whether the token representing the operator is a symbol or a name.
You use the prefix format or binary format whenever the operator token is a symbol. (The functional format is never used with symbols.)
You use the functional format whenever the operator token is a name.
Note: Binary format can be used instead if the operator is binary.
The following table summarizes these rules:
Operator
Token
One Operand
(Unary Operator)
Two Operands
(Binary Operator)
Three or More Operands
Symbol
Prefix format.
!available
Binary format.
rate + 35
Not applicable.
Name
Functional format.
NOT(available)
COUNT(servRecs)
Functional format.
PLUS(rate, 35)
 
Binary format.
rate PLUS 35
Functional format.
PLUS(rate, 35, 100)
OF(3, models, doors > 2)
With operators such as AND, OR, XOR, PLUS, and MULTIPLY, you can specify an arbitrary number of operands in functional format, or you can repeatedly nest binary-format expressions.
Nested Binary-Format Expressions
Equivalent Functional Format
a AND b AND c
a && b && c
AND(a, b, c)
1 PLUS 2 PLUS 3
1 + 2 + 3
PLUS(1, 2, 3)
Note:Although 1 + 2 + 3 appears to be a symbol token used with three operands, it is in fact one binary expression nested in another. You can include parentheses to indicate this explicitly: (1+2)+3
Operator Precedence
DO supports complex expressions that combine and nest multiple operator expressions. Within such an expression, operators are evaluated in precedence order. Operators with higher precedence are evaluated before operators with lower precedence. Operators with equivalent precedence are in the same associativity group, and are usually evaluated from left to right.
You use parentheses to clarify the precedence order or to override it. For example, the multiplication operator has higher precedence than the addition operator, so the numeric expression 3+2*5 is evaluated as 3+(2*5). To override this, you specify (3+2)*5.
The following table lists DO operators in precedence order. The operators at the top of the list have higher precedence and are evaluated first; operators in the same associativity grouping have equivalent precedence.
 
Operator
Description
Associativity
.
::
()
Class scope, grouping
Left to right
operator()
 
Operators in functional format
[n]
[n:m]
[predicate]
Subscript operators
!
NOT
Logical negation
Right to left
 
~
 
Bitwise complement
*
/
Arithmetic multiplication, division
Left to right
%
 
Arithmetic modulus
+
-
Arithmetic addition, subtraction
Unary plus, minus
Left to right
<<
>>
Bitwise shift left, right
Left to right
<
>
<=
>=
Relational less than, greater than
Left to right
==
!=
Relational equality, inequality
Left to right
=~
=~~
!~
!~~
String regular expression
&
 
Left to right
^
 
Bitwise exclusive OR
Left to right
|
 
Bitwise inclusive OR
Left to right
&&
AND
Left to right
^^
XOR
Logical exclusive OR
Left to right
||
OR
Logical inclusive OR
Left to right