Boolean Logic
Luma provides a complete set of comparison and logical operators for building conditions and making decisions.
Comparison operators
| Operator | Meaning | Example |
|---|---|---|
== |
Equal | a == b |
!= |
Not equal | a != b |
< |
Less than | a < b |
> |
Greater than | a > b |
<= |
Less than or equal | a <= b |
>= |
Greater than or equal | a >= b |
All comparison operators return a bool value (true or false).
x: int = 10
print(x == 10) // true
print(x != 5) // true
print(x < 20) // true
print(x >= 10) // trueLogical operators
| Operator | Meaning | Example |
|---|---|---|
&& |
Logical AND | a && b |
|| |
Logical OR | a || b |
! |
Logical NOT | !a |
a: bool = true
b: bool = false
print(a && b) // false
print(a || b) // true
print(!a) // false
print(!b) // trueOperator precedence
Operators evaluate in a specific order, from highest to lowest priority:
| Priority | Operators | Category |
|---|---|---|
| Highest | *, /, % |
Arithmetic |
+, - |
Arithmetic | |
==, !=, <, >, <=, >= |
Comparison | |
&& |
Logical AND | |
| Lowest | || |
Logical OR |
This means comparisons are evaluated before logical operators, so you can write natural conditions without parentheses:
if age >= 18 && age <= 65 {
print("Working age")
}This is equivalent to (age >= 18) && (age <= 65).
Use parentheses when you want to override the default order:
if (a || b) && c {
// OR is evaluated first because of parentheses
}Short-circuit evaluation
Logical operators use short-circuit evaluation:
&&stops at the firstfalsevalue (the right side is not evaluated)||stops at the firsttruevalue (the right side is not evaluated)
// safe — the second condition is only checked if the first passes
if list.len() > 0 && list[0] == "admin" {
print("Admin access")
}Combining conditions
You can chain as many logical operators as needed:
if is_active && !is_banned && (role == "admin" || role == "moderator") {
print("Access granted")
}No implicit truthiness
Luma does not treat non-boolean values as true or false. You must write explicit comparisons:
count: int = 5
name: str = ""
// These do NOT work:
// if count { ... }
// if name { ... }
// Write explicit comparisons instead:
if count > 0 {
print("Has items")
}
if name != "" {
print("Has name")
}Nil comparisons
You can compare values to nil to check for optional (nullable) values:
x: ?int = "3".to_int()
if x != nil {
print("Got a value")
}When comparing an optional type with a non-nil value, Luma automatically handles the nil check:
x: ?int = "3".to_int()
if x > 2 {
print("Greater than 2")
}
// Compiles safely — if x is nil, the condition is falseNumeric type coercion
When comparing an int with a float, the int is automatically promoted to float:
score: int = 7
if score > 3.5 {
print("Above average")
}Negation
The ! operator negates a boolean expression:
is_done: bool = false
if !is_done {
print("Still working")
}It can be applied to any expression that returns a bool:
if !list.contains(42) {
print("42 not found")
}Quick reference
// Comparisons
a == b // equal
a != b // not equal
a < b // less than
a > b // greater than
a <= b // less or equal
a >= b // greater or equal
// Logical
a && b // both true
a || b // either true
!a // negation
// Combined
x > 0 && x < 100 // range check
name == "A" || name == "B" // multiple values
!(a && b) // negate compound