Operators
JOEL supports various operators for different operations.
Arithmetic Operators
let a = 10
let b = 3
a + b # Addition: 13
a - b # Subtraction: 7
a * b # Multiplication: 30
a / b # Division: 3 (integer) or 3.33... (float)
a % b # Modulo: 1Type Behavior
let x: i32 = 10
let y: i32 = 3
let result: i32 = x / y # Integer division: 3
let a: f64 = 10.0
let b: f64 = 3.0
let result: f64 = a / b # Float division: 3.333...Comparison Operators
let a = 10
let b = 5
a == b # Equal: false
a != b # Not equal: true
a > b # Greater than: true
a < b # Less than: false
a >= b # Greater or equal: true
a <= b # Less or equal: falseLogical Operators
let x = true
let y = false
x && y # AND: false
x || y # OR: true
!x # NOT: falseShort-Circuit Evaluation
# && stops if first is false
false && expensive_function() # expensive_function not called
# || stops if first is true
true || expensive_function() # expensive_function not calledAssignment Operators
let x = 10
x += 5 # x = x + 5: 15 (coming soon)
x -= 3 # x = x - 3: 7 (coming soon)
x *= 2 # x = x * 2: 20 (coming soon)
x /= 2 # x = x / 2: 10 (coming soon)
x %= 3 # x = x % 3: 1 (coming soon)Note: Currently use explicit assignment: x = x + 5
String Operators
Concatenation
let first = "Hello"
let second = "World"
let greeting = first + " " + second # "Hello World"String Interpolation
# Coming soon
let name = "JOEL"
let greeting = "Hello, ${name}!" # "Hello, JOEL!"Member Access
let person = {"name": "JOEL", "age": 24}
let name = person["name"] # Access map value
# Coming soon
let name = person.name # Dot notationIndex Access
let list = [1, 2, 3, 4, 5]
let first = list[0] # Access: 1
let last = list[4] # Access: 5Function Call
fn add(a: i32, b: i32) -> i32 {
return a + b
}
let result = add(5, 3) # Call function: 8Operator Precedence
Operators are evaluated in this order (highest to lowest):
- Parentheses:
() - Member/Index Access:
.,[] - Unary:
!,- - Multiplicative:
*,/,% - Additive:
+,- - Comparison:
==,!=,<,>,<=,>= - Logical AND:
&& - Logical OR:
|| - Assignment:
=,+=,-=, etc.
Examples
let result = 2 + 3 * 4 # 14 (not 20)
let result = (2 + 3) * 4 # 20
let result = !true && false # false
let result = 5 > 3 && 2 < 4 # trueType-Specific Operators
Numeric
let x: i32 = 10
let y: i32 = 3
x + y # 13
x - y # 7
x * y # 30
x / y # 3
x % y # 1Boolean
let a = true
let b = false
a && b # false
a || b # true
!a # falseString
let s1 = "Hello"
let s2 = "World"
s1 + " " + s2 # "Hello World"Examples
Calculator
let a = 15
let b = 3
print("a + b =", a + b) # 18
print("a - b =", a - b) # 12
print("a * b =", a * b) # 45
print("a / b =", a / b) # 5
print("a % b =", a % b) # 0Comparisons
let x = 10
let y = 5
print("x > y:", x > y) # true
print("x < y:", x < y) # false
print("x == y:", x == y) # false
print("x != y:", x != y) # trueLogical Operations
let age = 25
let has_license = true
let can_drive = age >= 18 && has_license # true
let is_minor = age < 18 || !has_license # false