Operators
Arithmetic, comparison, logical, compound assignment, and increment/decrement operators in Soli.
Arithmetic Operators
Comparison Operators
Logical Operators
&&
Logical AND
Both conditions must be true.
let age = 25
let has_license = true
if age >= 18 && has_license
print("Can drive")
end
At least one condition must be true.
if is_weekend || is_holiday
print("Day off!")
end
Negates the condition.
if !is_raining
print("No umbrella needed")
end
String Concatenation.
# Concatenation
let greeting = "Hello, " + "World!"; # "Hello, World!"
let message = "Value: " + 42; # "Value: 42" (auto-conversion)
# String methods
let text = " Hello, World! "
print(text.trim()); # "Hello, World!"
print(text.lstrip()); # "Hello, World! "
print(text.rstrip()); # " Hello, World!"
print(text.upper()); # " HELLO, WORLD! "
print(text.lower()); # " hello, world! "
print(text.len()); # 18
# Substring operations
let s = "Hello, World!"
print(s.sub(0, 5)); # "Hello" (from index 0, length 5)
print(s.find("World")); # 7 (index of first occurrence)
print(s.contains("Hello")); # true
print(s.starts_with("Hell")); # true
print(s.ends_with("!")); # true
# String manipulation
print(s.replace("World", "Soli")); # "Hello, Soli!"
print(s.split(", ")); # ["Hello", "World!"]
print(s.chomp()); # "Hello, World!" (removes trailing newline)
print(s.reverse); # "!dlroW ,olleH"
print(s.capitalize()); # "Hello, world!"
print(s.swapcase()); # "hELLO, wORLD!"
# String padding
print("hi".center(10)); # " hi "
print("hi".ljust(10)); # "hi "
print("hi".rjust(10)); # " hi"
# String queries
print(s.count("l")); # 3
print(s.gsub("l", "L")); # "HeLLo, WorLd!"
print(s.match("H(.*)o")); # ["Hello", "ell"]
print(s.scan("[aeiou]")); # ["e", "o", "o"]
print(s.tr("aeiou", "AEIOU")); # "HEllO, WOrld!"
# Character operations
print("A".ord()); # 65
print("hello".bytes()); # [104, 101, 108, 108, 111]
print("hello".chars()); # ["h", "e", "l", "l", "o"]
print("a\nb\nc".lines()); # ["a", "b", "c"]
# Partition and delete
print("hello-world-test".partition("-")); # ["hello", "-", "world-test"]
print("hello-world".delete("l")); # "heo-word"
print("hello".delete_prefix("hel")); # "lo"
print("hello".delete_suffix("lo")); # "hel"
# String encoding
print("hello".bytesize()); # 5
# Truncate
print("hello world".truncate(5)); # "he..."
Use ?? to provide default values for null.
let user = {"name": "Alice", "email": null}
# Traditional null check
let email = user["email"]
if email == null
email = "unknown"
end
# Null coalescing operator
let display_email = user["email"] ?? "unknown"
# Chaining with null values
let city = user["address"]["city"] ?? "Unknown City"
# If any key in the chain is null/missing, returns "Unknown City"
Use &. for safe navigation — access properties or call methods on values that might be null without raising an error.
# Safe navigation operator
let user = get_user # might return null
# Returns null if user is null, otherwise returns user.name
let name = user&.name
# Chain safe navigation for nested access
let city = user&.address&.city
# Call methods safely — returns null if user is null
let greeting = user&.greet
# Combine with ?? for default values
let display_name = user&.name ?? "Anonymous"
Creates a range of integers (exclusive end).
# Range creates an array
let numbers = 1..5 # [1, 2, 3, 4]
# Use in for loops
for i in 0..10
print(i) # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
end
Compound Assignment Operators
Compound assignment operators combine an arithmetic operation with assignment. They modify the variable in-place.
| Operator | Description | Equivalent |
|---|---|---|
| += | Add and assign | a += b is a = a + b |
| -= | Subtract and assign | a -= b is a = a - b |
| *= | Multiply and assign | a *= b is a = a * b |
| /= | Divide and assign | a /= b is a = a / b |
| %= | Modulo and assign | a %= b is a = a % b |
let score = 0
score += 10 # score is now 10
score -= 3 # score is now 7
score *= 2 # score is now 14
score /= 7 # score is now 2
score %= 2 # score is now 0
# Also works with floats
let price = 19.99
price *= 0.9 # apply 10% discount
# String concatenation with +=
let msg = "Hello"
msg += " World" # msg is now "Hello World"
Increment & Decrement
Postfix ++ and -- operators modify a variable by 1 and return the old value.
| Operator | Description | Returns |
|---|---|---|
| ++ | Postfix increment | Old value (before increment) |
| -- | Postfix decrement | Old value (before decrement) |
let i = 0
i++ # i is now 1
i++ # i is now 2
let old = i-- # old is 2, i is now 1
# Common pattern: loop counter
let n = 0
while n < 5
println(n)
n = n + 1
end