Operators
Table
The table below contains the operators in NPL, the equivalent method name, a short explanation, an example, and a precedence level (where 1 indicates the highest precedence).
Most operators can be also expressed using a functional syntax, for example 2.plus(3)
is the same as 2 + 3
.
Operator | Method name | Explanation | Example | Precedence | Operates on |
---|---|---|---|---|---|
= |
N/A | assignment | = expr |
1 | any value |
! |
not |
logical complement (not) | !expr |
2 | Boolean |
- |
negative |
arithmetic negation | -expr |
2 | Number, Amount |
* |
multiplyBy |
arithmetic multiplication | expr * expr |
3 | Number and Number, Amount and Number |
/ |
divideBy |
arithmetic division | expr / expr |
3 | Number and Number, Amount and Number |
% |
remainder |
arithmetic division remainder | expr % expr |
3 | Number and Number, Amount and Number |
+ |
plus |
arithmetic addition | expr + expr |
4 | Number and Number, Amount and Amount |
+ |
plus |
concatenation | expr + expr |
4 | Text and Text, List and List, Set and Set |
+ |
plus |
time addition | expr + expr |
4 | DateTime and Duration, DateTime and Period |
- |
minus |
arithmetic subtraction | expr - expr |
4 | Number and Number, Amount and Amount |
- |
minus |
time subtraction | expr - expr |
4 | DateTime and Duration, DateTime and Period |
> |
greaterThan |
greater than comparison | expr > expr |
5 | Number and Number, Text and Text |
>= |
greaterThanOrEqual |
greater than or equal to comparison | expr >= expr |
5 | Number and Number, Text and Text |
< |
lessThan |
less than comparison | expr < expr |
5 | Number and Number, Text and Text |
<= |
lessThanOrEqual |
less than or equal to comparison | expr <= expr |
5 | Number and Number, Text and Text |
== |
N/A | equality comparison | expr == expr |
6 | any two values |
!= |
N/A | inequality comparison | expr != expr |
6 | any two values |
&& |
N/A | logical and | expr && expr |
7 | Boolean and Boolean |
|| |
N/A | logical or | expr || expr |
8 | Boolean and Boolean |
Equality
Equality comparisons for types that hold other types, such as Optional
and collections, and performed element-wise.
While this makes equality comparisons compatible with unions, it also means that in some cases types with different type
parameters may be considered equal.
Given
union U { Number, Text }
the following are all true
optionalOf<Number>(1) == optionalOf<U>(1)
optionalOf<Number>() == optionalOf<U>()
optionalOf<Number>() == optionalOf<Text>()
Remainder
The remainder operator (%
) supports negative dividend and divisor. The sign of the divisor does not affect the result.
The sign of the result is the same as the sign of the dividend. The following examples illustrate this.
47 % 10,
47 % -10,
7 // all equal
-47 % 10,
-47 % -10,
-7 // all equal
50 % 10,
50 % -10,
-50 % 10,
-50 % -10,
0 // all equal
5.8 % 2.2,
5.8 % -2.2,
1.4 // all equal
-5.8 % 2.2,
-5.8 % -2.2,
-1.4 // all equal
Operator context
Each operator's meaning in the context of types is described with the type. Parties have their own operators, and are described in party expressions.