Operators
An operator in Carrot is one or more symbols or words that compare the value of a field on its left with one or more values (or functions) on its right. The valuation of the clause will always result as true.
Carrot operators understand money-math and automatically convert currencies when adding or subtracting two different currencies.
Name | Function |
+ (add) | An operator used to add two or more expressions. |
| An operator used to subtract an expression from another expression. |
An operator used to multiply two or more expressions. | |
An operator used to divide one expression by another. | |
An operator used to calculate the remainder of a division operation. | |
Returns true when the value of a given field and a given expression are the same. | |
Returns all records where the value of a given field and a given expression are not the same. | |
! (not) | Invalidates the truthiness of an evaluated expression. |
Returns all records where the value of one expression or field is greater than the value of another expression or field. | |
Returns all records where the value of one expression or field is greater than or equal to the value of another expression or field. | |
Returns all records where the value of one expression or field is less than the value of another expression or field. | |
Returns all records where the value of one expression or field is less than or equal to the value of another expression or field. | |
Access a given expression as an array, and return a defined position on that array. | |
Returns true where the value of a given field and a given expression partially match. | |
&& (and) | A logic operator used to evaluate the truthiness of two or more expressions. |
|| (or) | A logic operator used to evaluate the truthiness of at least one expression in a sequence of expressions. |
if | A logic operator used to return a defined expression based on the truthiness of one or more other expressions. |
Evaluates the truthiness of an expression, and returns the value of one of two expressions based on that outcome. | |
Evaluates the truthiness of the first expression, and returns its value if that value is truthy. If the value of the first expression is not truthy, it returns the value of the second expression. | |
. (dot) | Used to retrieve a property of a given field. |
When an expression makes use of more than one operator, Carrot relies on a set of rules to determine the order in which each operator is evaluated. Carrot follows a standard order of operations when evaluating an expression based on operator precedence. Operators that have a higher precedence in the table below will be evaluated first.
Precedence | Operator | Associativity |
9 | [ ](array) . (dot) | left to right |
8 | ! (not) | right to left |
7 | left to right | |
6 | left to right | |
5 | not associative | |
4 | left to right | |
3 | && (and) | left to right |
2 | || (or) | left to right |
1 | right to left |
Consider the expression below.
Without operator precedence, Carrot would not know which operation to complete first. If we refer to the table above, we see that multiplication has a higher precedence than addition. Thus, variableA * variableB is evaluated first, then the result of that expression is added to variableC.
Of course, addition and multiplication are two well known examples of operator precedence in arithmetic, and it works in the same way in Carrot. Just like arithmetic, if you wanted to change the precedence of the above expression, you would use parenthesis as illustrated below.
In the above expression, we are telling Carrot to explicitly add variableB to variableC before multiplying the result by variableA.