A comprehensive guide to all operators in Pome, their usage, and precedence.
Arithmetic operators perform mathematical calculations:
var a = 10;
var b = 3;
print(a + b); // Addition: 13
print(a - b); // Subtraction: 7
print(a * b); // Multiplication: 30
print(a / b); // Division: 3.333...
print(a % b); // Modulo (remainder): 1
Division returns a decimal result:
print(10 / 3); // Output: 3.333...
print(10 / 2); // Output: 5
print(-10 / 3); // Output: -3.333...
Modulo returns the remainder:
print(10 % 3); // Output: 1
print(15 % 5); // Output: 0
print(-10 % 3); // Output: -1
Comparison operators return boolean values:
print(5 == 5); // Equal: true
print(5 == 3); // Equal: false
print(5 != 3); // Not equal: true
print(5 > 3); // Greater than: true
print(5 < 3); // Less than: false
print(5 >= 5); // Greater or equal: true
print(3 <= 5); // Less or equal: true
Pome does not perform automatic type coercion for comparisons. When comparing values of different types, the comparison will generally evaluate to false unless values are explicitly converted to compatible types first. For predictable results, ensure both sides of a comparison have the same type.
print(5 == "5"); // false (different types)
print(true == 1); // false (different types)
print(nil == false); // false
Logical operators work with boolean values:
var a = true;
var b = false;
print(a and b); // Logical AND: false
print(a or b); // Logical OR: true
print(not a); // Logical NOT: false
Returns true only if both operands are true:
print(true and true); // true
print(true and false); // false
print(false and true); // false
print(false and false); // false
Short-circuits: If the first operand is false, the second is not evaluated.
var x = nil;
// Safe: len(x) is not evaluated because x != nil is false
if (x != nil and len(x) > 0) {
// ...
}
Returns true if at least one operand is true:
print(true or true); // true
print(true or false); // true
print(false or true); // true
print(false or false); // false
Short-circuits: If the first operand is true, the second is not evaluated.
var x = nil;
// Safe: isValid(x) is only called if x is NOT nil (Wait, logic check: if x != nil is true, it short circuits. If false, it evals right. So x IS nil on right. Bad example for OR short circuit safety unless inverted).
// Better example:
var config = {debug: true};
// If config.debug is true, the second part is skipped.
if (config.debug or expensiveCheck()) {
print("Debug or check passed");
}
Inverts a boolean value:
print(not true); // false
print(not false); // true
print(not nil); // true (nil is falsy)
print(not 0); // true (0 is falsy)
The + operator concatenates strings:
print("Hello" + " " + "World"); // Output: Hello World
print("Count: " + 5); // Output: Count: 5
The = operator assigns values to variables:
var x = 10;
x = 20;
Pome supports compound assignment operators for concise updates:
| Operator | Description | Equivalent |
|---|---|---|
+= |
Add and assign | x = x + y |
-= |
Subtract and assign | x = x - y |
*= |
Multiply and assign | x = x * y |
/= |
Divide and assign | x = x / y |
%= |
Modulo and assign | x = x % y |
var x = 10;
x += 5; // x is now 15
x *= 2; // x is now 30
Operators are evaluated in this order (highest to lowest):
| Precedence | Operator | Associativity |
|---|---|---|
| 1 | () [] . |
Left to right |
| 2 | ! - (unary) |
Right to left |
| 3 | ^ |
Left to right |
| 4 | * / % |
Left to right |
| 5 | + - |
Left to right |
| 6 | < <= > >= |
Left to right |
| 7 | == != |
Left to right |
| 8 | and |
Left to right |
| 9 | or |
Left to right |
| 10 | ?: (ternary) |
Right to left |
| 11 | = += -= *= /= %= |
Right to left |
// Multiplication before addition
print(2 + 3 * 4); // Output: 14 (not 20)
// Parentheses override precedence
print((2 + 3) * 4); // Output: 20
// Comparison before AND
print(true and 5 > 3); // Output: true
// AND before OR
print(true or false and false); // Output: true (and is evaluated first)
print((true or false) and false); // Output: false
Negates a number:
var x = 10;
print(-x); // Output: -10
print(-(-x)); // Output: 10
Inverts a boolean:
print(!true); // Output: false
print(!false); // Output: true
The conditional operator chooses between two values:
var age = 25;
var status = age >= 18 ? "adult" : "minor";
print(status); // Output: adult
Syntax: condition ? valueIfTrue : valueIfFalse
var score = 85;
var grade = score >= 90 ? "A" :
score >= 80 ? "B" :
score >= 70 ? "C" :
score >= 60 ? "D" : "F";
print(grade); // Output: B
print("10" == 10); // May vary
print("abc" == 123); // false
print(nil == nil); // true
print(nil == false); // false
print(nil == 0); // false
var value = 50;
if (value > 0 and value < 100) {
print("In range");
}
var x = 5;
if (x > 0 and x < 10 or x == 100) {
print("Valid");
}
var user = {};
if (user != nil and user.name != nil) {
print(user.name);
}
var isActive = true;
isActive = not isActive; // Now false
var input = nil;
var result = input != nil ? input : "default";
-
Use parentheses for clarity: Even when not required, they make code easier to read
// Less clear if (x > 5 and y < 10 or z == 3) { } // Clearer if ((x > 5 and y < 10) or z == 3) { } -
Be aware of operator precedence: Unexpected results come from forgetting it
print(2 + 3 * 4); // 14, not 20 -
Use short-circuit evaluation strategically:
// This won't crash if x is nil if (x != nil and x > 10) { } -
Be explicit when converting types:
// Pome does not implicitly convert non-string values when concatenating. // Ensure `myVar` is a string before concatenation. print("Value: " + myVar); // myVar must be a string
Next: Module System
Back to: Documentation Home