Appearance
Logical Operators
Overview
Logical operators in the expression language allow you to combine multiple conditions into complex boolean expressions. The language supports three fundamental logical operations:
and
- Logical AND operatoror
- Logical OR operator- Parentheses
()
- For grouping and controlling precedence
Core Concepts
1. Basic Operators
AND Operator
The and
operator returns true only if both operands evaluate to true.
js
// Both conditions must be true
age > 18 and status == "active"
// Multiple AND conditions
category == "premium" and price > 100 and stock > 0
Truth table for AND:
Left Right Result
true true true
true false false
false true false
false false false
OR Operator
The or
operator returns true if at least one of the operands evaluates to true.
js
// Either condition can be true
status == "active" or status == "pending"
// Multiple OR conditions
priority == "high" or severity == "critical" or @isUrgent(task)
Truth table for OR:
Left Right Result
true true true
true false true
false true true
false false false
2. Operator Precedence
The expression language follows a strict operator precedence order:
- Parentheses
()
- NOT
- AND
- OR
This means that:
js
// This expression:
status == "active" and role == "admin" or department == "IT"
// Is evaluated as:
(status == "active" and role == "admin") or department == "IT"
3. Expression Grouping
Parentheses can be used to:
- Override default operator precedence
- Group related conditions
- Create clear, readable expressions
Examples:
js
// Default precedence
age > 18 and role == "user" or status == "special"
// Evaluates as: (age > 18 and role == "user") or status == "special"
// Modified precedence with parentheses
age > 18 and (role == "user" or status == "special")
// Evaluates as specified by the parentheses
4. Complex Expressions
You can create sophisticated logical conditions by combining operators:
js
// Multiple conditions with different operators
(status in ("active", "pending")) and
(priority > 3 or @isUrgent(task))
// Nested conditions
((age > 18 and role == "user") or
(age > 21 and role == "admin")) and
status not in ("blocked", "deleted")
// Combining with functions
@hasPermission(user, "edit") and
(status == "draft" or @isOwner(user, document))
Implementation Details
Evaluation Process
The expression is parsed into a binary tree structure where:
- Leaf nodes are individual conditions
- Internal nodes are logical operators (AND/OR)
- Each node implements the Evaluate() method
Evaluation follows these rules:
- Short-circuit evaluation is used for efficiency
- For AND: if left operand is false, right is not evaluated
- For OR: if left operand is true, right is not evaluated
Error Handling
The evaluation process handles several types of errors:
Syntax Errors:
js// Missing right operand status == "active" and // Invalid operator combination status == "active" and and role == "admin"
Type Errors:
js// Mixing incompatible types age > "18" and status == true // age should be compared with number
Evaluation Errors:
js// Undefined field access nonexistent.field == "value" and status == "active"
Best Practices
Use Parentheses for Clarity
js// Clear intention (status == "active" and priority > 3) or (status == "pending" and @isUrgent(task))
Break Complex Expressions
js// Instead of one long expression: (status == "active" and priority > 3) or (status == "pending" and @isUrgent(task)) or (category == "special" and @hasFlag(item)) // Consider splitting into multiple conditions isHighPriority = status == "active" and priority > 3 isPending = status == "pending" and @isUrgent(task) isSpecial = category == "special" and @hasFlag(item) isHighPriority or isPending or isSpecial
Leverage Short-Circuit Evaluation
js// Put cheaper operations first @isSimpleCheck(item) and @expensiveOperation(item)
Maintain Readability
js// Instead of status=="active"and priority>3or category=="urgent" // Write status == "active" and priority > 3 or category == "urgent"
Common Patterns
Permission Checking
js@hasRole(user, "admin") or (@hasRole(user, "editor") and status == "draft")
Status Validation
jsstatus in ("active", "pending") and (priority > 3 or @hasFlag(item, "urgent"))
Complex Business Rules
js(category == "premium" and price > 100) and (stock > 0 or @isPreorder(item)) and not @isRestricted(item)