Operators
Arithmetic, Logical and Comparison Operators
Here is a list of the available operators in a SIL™ program and detailed usage information.
Arithmetic operators
In version 2.5 (and 3.0 yet again) we have reworked the way operators handle different types and conversions by putting an emphasis on the left-hand side (LHS) value of the operation. Consequently, only the right-hand side (RHS) operand will be converted (if necessary) to a type that is valid for the current operation with the LHS type.
For each operator and LHS type, we define two lists: valid types and convertible types. The list of convertible types is based on Type Conversion. This creates three cases:
If the RHS type is one of the valid types, then the operation will proceed as it is and the result calculated according to the table below.
If the RHS type is one of the convertible types, then it will attempt to convert it to one of the valid types (in the order the valid types are presented in the table below) until the conversion succeeds and then proceeds according to case 1.
If the RHS type is neither valid nor convertible, the program will end in error.
Operator | LHS type | RHS types | Result | Explanations and Usage |
---|---|---|---|---|
+ | number | number | number | standard addition of numbers |
integer | number | standard addition of numbers | ||
integer | integer | integer | standard addition of integers | |
number | integer | standard addition of integers | ||
string | string | string | adds the string representation of the RHS operand at the end of the LHS string | |
boolean | Unsupported | |||
date | interval | date | adds the interval to the date and returns the result | |
interval | interval | interval | adds the two intervals and returns the result | |
date | date | adds the interval to the date and returns the result | ||
<type> [] | <type> | <type> [] | adds the RHS value to the LHS array, where <type> can be any type. | |
- | number | number | number | standard subtraction of numbers |
integer | number | standard subtraction of numbers | ||
integer | integer | integer | standard subtraction of integers | |
number | integer | standard subtraction of integers | ||
string | string | string | removes all occurrences of the RHS string in the LHS value | |
boolean | Unsupported | |||
date | date | interval | returns the interval difference between the two dates | |
interval | date | subtracts the RHS interval from the date and returns the value | ||
interval | interval | interval | standard interval subtraction | |
<type>[] | <type> | <type>[] | removes the first occurrence of the RHS value from the array; <type> can be any type. | |
* | number | number | number | standard multiplication of numbers |
integer | number | standard multiplication of numbers | ||
interval | interval | multiplies the interval by the number of times specified by the LHS operand and returns the result | ||
number [] | number | number [] | multiplies each value in the LHS array with the number in the RHS operand and returns the result | |
integer | number [] | multiplies each value in the LHS array with the integer in the RHS operand and returns the result | ||
integer | integer | integer | standard multiplication of integers | |
number | integer | standard multiplication of integers | ||
interval | interval | multiplies the interval by the integer of times specified by the LHS operand and returns the result | ||
integer [] | integer | integer [] | multiplies each value in the LHS array with the integer in the RHS operand and returns the result | |
number | integer [] | multiplies each value in the LHS array with the number in the RHS operand and returns the result | ||
string | Unsupported | |||
boolean | Unsupported | |||
date | Unsupported | |||
interval | number | interval | multiplies the interval by the number of times specified by the RHS operand and returns the result | |
<type>[] (except number) | Unsupported | |||
/ | number | number | number | standard division of numbers |
integer | number | standard division of numbers | ||
number [] | number | number [] | divides each value in the LHS array by the number in the RHS operand and returns the result | |
integer | number [] | divides each value in the LHS array by the integer in the RHS operand and returns the result | ||
integer | integer | integer | standard division of integers | |
number | integer | standard division of integers | ||
integer [] | integer | integer [] | divides each value in the LHS array by the integer in the RHS operand and returns the result | |
number | integer [] | divides each value in the LHS array by the number in the RHS operand and returns the result | ||
string | Unsupported | |||
boolean | Unsupported | |||
date | Unsupported | |||
interval | number | interval | divides the interval by the number specified in the RHS operand | |
<type>[] (except number) | Unsupported | |||
% | number | number | number | standard modulo division of numbers |
integer | number | standard modulo division of numbers | ||
number [] | number | number [] | applies the modulo operator on each number in the LHS array with the number in the RHS operand and returns the results | |
integer | number [] | applies the modulo operator on each number in the LHS array with the integer in the RHS operand and returns the results | ||
integer | integer | integer | standard modulo division of integers | |
number | integer | standard modulo division of integers | ||
integer [] | integer | integer [] | applies the modulo operator on each number in the LHS array with the integer in the RHS operand and returns the results | |
number | integer [] | applies the modulo operator on each number in the LHS array with the number in the RHS operand and returns the results | ||
string | Unsupported | |||
boolean | Unsupported | |||
date | Unsupported | |||
interval | Unsupported | |||
<type>[] (except number) | Unsupported |
You can also use the combined forms of the arithmetic operators with the attrib operator: +=, -=, *= and /=.
string s = "a";
s = s + "b";
// is equivalent with
s += "b";
Numbers also support the pre/post increment/decrement operators.
--i;
i--;
++i;
i++;
Comparison operators
Operator | Operands | Return | Explanations and Usage |
---|---|---|---|
== (alias 'eq') | Right operand will be casted to the left operand's type if their types are different | true/false | Returns true if the values are equal and false otherwise. |
!= (alias 'neq') | Right operand will be casted to the left operand's type if their types are different | true/false | Returns false if the values are equal and true otherwise. |
< (alias 'lt') | Right operand will be casted to the left operand's type if their types are different | true/false | Returns true if the first value is lower than the second one and false otherwise. |
> (alias 'gt') | Right operand will be casted to the left operand's type if their types are different | true/false | Returns true if the first value is greater than the second one and false otherwise. |
<= (alias 'le') | Right operand will be casted to the left operand's type if their types are different | true/false | Returns true if the first value is lower then or equal to the second one and false otherwise. |
>= (alias 'gt') | Right operand will be casted to the left operand's type if their types are different | true/false | Returns true if the first value is greater than or equal to the second one and false otherwise. |
Logical operators
Operator | Operands | Return | Explanations and Usage |
---|---|---|---|
&& (alias 'and') | boolean && boolean | true/false | Logical "and" |
|| (alias 'or') | boolean || boolean | true/false | Logical "or" |
! (alias 'not') | boolean | true/false | Logically complements of the specified boolean. |
! (alias 'not') | number | number | Complements the number in relation to 0. (returns number * -1) |
Ternary operators
Operator | Operands | Return | Explanations and Usage |
---|---|---|---|
?: | boolean, <type>, <type> | <type> | <condition> ? <ifTrueValue> : <ifFalseValue> If <condition> is true, returns <ifTrueValue>, otherwise returns <ifFalseValue>. Note that <ifTrueValue> and <ifFalseValue> must have same type. |
The Indexing Operator
The basic use of the indexing operator (introduced in version 2.5) is to allow you to easily access the contents of a list.
General syntax
string [] arr = {"a", "b", "c"};
string contents = arr[0] + arr[1] + arr[2] + arr[3]; // as user friendly as arrayGetElement, if index is out of bounds, will return an empty value.
arr[3] = "d";
Moreover, it allows you to create key-value lists (maps) and retrieve values from them by using a string inside the operator (instead of a number).
number [] map;
map["one"] = 1;
map["two"] = 2;
print(map["one"]);
// note that when used in a for-loop, map only has 2 elements: 1 and 2. The keys will be hidden.
print(map); // will print 1|2
When accessing a map as a list, the contents are retrieved in the order they were added. You can still use the operator with an integer value even on maps.
date [] days;
days["yesterday"] = currentDate() - "1d"; // added as first element
days["today"] = currentDate(); // added as second element
days["tomorrow"] = currentDate() + "1d"; // added as third element
if(days["today"] == days[1]){
print("Today is the second element in the array"); // yes, this will be printed
}
The indexing operator can also be used with certain simple types (not arrays) and a specific value to provide additional functionality.
SIL™ type | Index Value | Get | Get Return Type | Set |
---|---|---|---|---|
string | a number | Gets the character at the specified index in the string | string | Inserts a character at the specified index. If given a longer string, it will insert the first character of the string. |
date | "DAY" | Gets the day of month from the date | integer | Unsupported |
"MONTH" | Gets the month from the date | integer | Unsupported | |
"YEAR" | Gets the year from the date | integer | Unsupported | |
"HOUR" | Gets the hour (0-23) from the date | integer | Unsupported | |
"MINUTE" | Gets the minute (0-59) from the date | integer | Unsupported | |
"SECOND" | Gets the seconds (0-59) from the date | integer | Unsupported | |
"MILLISECOND" | Gets the milliseconds (0-999) from the date | integer | Unsupported | |
"WEEK" | Gets the week number within the current year | integer | Unsupported | |
"WEEKINMONTH" | Gets the week number within the current month | integer | Unsupported | |
"TOMILLIS" | Gets the current time as UTC milliseconds from the epoch. | integer | Unsupported | |
"DAYOFWEEK" | Gets the three letter format for the day of the week (e.g. "Mon", "Tue", "Wed" etc) | string | Unsupported | |
"MONTHNAME" | Gets the three letter format for the name of the month (e.g. "Jan", "Feb", "Mar", etc.) | string | Unsupported | |
interval | "WEEK" | Gets the number of whole weeks inside the interval (e.g. interval i = "1w 14d"; i["WEEK"] will return 3) | integer | Unsupported |
"DAY" | Gets the number of whole days inside the interval (e.g. interval i = "1d 48h"; i["DAY"] will return 3) | integer | Unsupported | |
"HOUR" | Gets the number of whole hours inside the interval (e.g. interval i = "1h 120m"; i["HOUR"] will return 3) | integer | Unsupported | |
"MINUTE" | Gets the number of whole miuntes inside the interval (e.g. interval i = "1m 120s"; i["MINUTE"] will return 3) | integer | Unsupported | |
"SECOND" | Gets the number of seconds inside the interval (e.g. interval i = "1h 1m 3s"; i["SECOND"] will return 3) | integer | Unsupported | |
"TOMILLIS" | Gets the number of milliseconds inside the interval | integer | Unsupported |