Operators
The operators serve to perform operations in the same way as functions do, but using operators, your code is more compact and legible.
Operators can be arithmetic, relational and logical. The arithmetic operators can be used in all expressions, not only the logical ones. The relational and logical operators serve to create expressions with resulting boolean value.
All operators can be grouped into four categories:
Arithmetic Operators (\+ - * / % ++ --)
Relational Operators (> >= == <= < != ~= ?=)
Logical Operators (&& || ! == !=)
Assignment Operator (= += -= *= /= %=)
Arithmetic Operators
The arithmetic operators perform basic mathematical operation (addition, subtraction, etc.), concatenate strings or lists or merge content of two maps.
The operators can be used more times in one expression. The result depends on the order of operators within the expressions. In such a case, you can express priority of operations by parentheses.
Addition
The operator + serves to sum the values of two expressions, concatenate two string values, concatenate two lists or merge content of two maps.
Nevertheless, if you want to add any data type to a string, the second data type is converted to a string automatically and it is concatenated with the first (string) summand. But remember that the string must be on the first place.
Naturally, two strings can be summed in the same way.
The addition of two boolean values or two date data types is not possible. To create a new value from two boolean values, you must use logical operators instead.
If you concatenate several strings, use the following approach instead of the plus sign:
Subtraction and Unitary minus
The operator - subtracts one numeric data type from another.
If the numeric types of operands differ, firstly, automatic conversions are applied and then subtraction is performed.
Multiplication
The operator * multiplies two numbers.
Numbers can be of different data types. If data types of operands differ, automatic conversion is applied.
Division
Operator / serves to divide two numeric data types. Remember that you must not divide by zero. Division by zero throws TransformLangExecutorRuntimeException or returns Infinity
(in the case of double (number)
data type).
Modulus
Operator % returns the remainder of division. The operator can be used for floating-point, fixed-point and integral data types.
Incrementing
Operator ++ serves to increment numeric data type value by one. The operator can be used for both floating-point data types and integer data types.
If it is used as a prefix, the number is incremented first and then it is used in the expression.
If it is used as a postfix, first, the number is used in the expression and then it is incremented.
Remember that the incrementing operator cannot be applied on literals, record fields, map, or list values of integer data type.
Decrementing
Operator -- serves to decrement numeric data type value by one. The operator can be used for floating-point, fixed-point and integral data types. If it is used as a prefix, the number is decremented first and then it is used in the expression. If it is used as a postfix, first, the number is used in the expression and then it is decremented.
Remember that the decrementing operator cannot be applied on literals, record fields, map, or list values of integer data type.
Relational Operators
The following operators serve to compare some subexpressions when you want to obtain a boolean value result. Each of the mentioned signs can be used. These signs can be used more times in one expression. In such a case you can express priority of comparisons by parentheses.
If you choose the .operator.
syntax, operator must be surrounded by white spaces. Example syntax for the eq
operator:
5 .eq. 3
✓
5 == 3
✓
5 eq 3
x
5.eq(3)
x
Greater than Each of the two signs below can be used to compare expressions consisting of numeric, date and string data types. Both data types in the expressions must be comparable. The result can depend on the order of the two expressions if they are of different data types.
Greater than or equal to Each of the three signs below can be used to compare expressions consisting of the numeric, date and string data types. Both data types in the expressions must be comparable. The result can depend on the order of the two expressions if they are of different data types.
Less than Each of the two signs below can be used to compare expressions consisting of numeric, date and string data types. Both data types in the expressions must be comparable. The result can depend on the order of the two expressions if they are of different data types.
Less than or equal to Each of the three signs below can be used to compare expressions consisting of the numeric, date and string data types. Both data types in the expressions must be comparable. The result can depend on the order of the two expressions if they are of different data types.
Equal to Each of the two signs below can be used to compare expressions of any data type. Both data types in the expressions must be comparable. The result can depend on the order of the two expressions if they are of different data types.
Not equal to Each of the three signs below can be used to compare expressions of any data type. Both data types in the expressions must be comparable. The result can depend on the order of the two expressions if they are of different data types.
"typeof" Operator
Tests if a value (left operand) is of the specified type (right operand).
Returns false if the value is null
.
For lists and maps, does not check the type of elements.
Example 69. Usage of typeof
Logical Operators
If the expression whose value must be of boolean data type is complex, it can consist of some subexpressions (see above) that are put together by logical conjunctions (AND, OR, NOT, .EQUAL TO, NOT EQUAL TO). If you want to express priority in such an expression, you can use parentheses. From the conjunctions mentioned below, you can choose either form (for example, && or and, etc.).
Every sign of the form .operator.
must be surrounded by a white space.
Logical AND
Logical OR
Logical NOT
Logical EQUAL TO
Logical NOT EQUAL TO
Assignment Operator
Assignment operator assigns a value of expression on the right side of the operator to a variable on the left side of the operator.
Compound Operators
Compound operators allow you to use a variable as an accumulator.
CTL2 supports the following compound assignment operators: += (addition, string concatenation, list concatenation and map union), -= (subtraction), *= (multiplication), /= (division), and %= (modulus).
If the original value of the left-hand side variable is null
, the default value for the target type (0, empty string, empty list, empty map) is used for the evaluation instead. See variables ns
and ns2
in the example below.
Example 70. Compound assignment operators
CTL2 does not perform any counter-intuitive conversion of the right operand of +=. If you need to add double to integer, you should convert it explicitly:
It works with **-=, *=, /= ** and %= as well.
The = operator does not just pass object references, but performs a deep copy of values. That is of course more demanding in terms of performance. Deep copy is only performed for mutable data types, i.e. lists, maps, records and dates. Other types are considered immutable, as CTL2 does not provide any means of changing the state of an existing object (even though the object is mutable in Java). Therefore it is safe to pass a reference instead of copying the value. Note that this assumption may not be valid for custom CTL2 function libraries. Example 71. Modification of a copied list, map and record
Ternary Operator
Ternary operator is a compact conditional assignment.
It serves to set a value of a variable depending on a boolean expression or a boolean variable.
The expression above is same as:
The a
, c
and d
variables must be of the same data type (or type of c
and d
must be convertible to type of a
using automatic conversion). The b
variable is boolean
.
b, c
or d
do not have to be variables. They may be constants or expressions. c has to be a variable.
For example, you can use a ternary operator to assign minimum of c
and d
into a
in a compact way:
Conditional Fail Expression
The conditional fail expression allows the user to conditionally execute a piece of code depending on a failure occurred in the previous part of the code. variable = expr1 : expr2 : …​ : exprN;
integer count = getCachedValue() : refreshCacheAndGetCachedValue() : defaultValue;
Conditional expression is available only in an interpreted mode. It is not available in a compiled mode.
Last updated