Operators
Arithmetic, comparison, logical, and string 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");
}
||
Logical OR
At least one condition must be true.
if (is_weekend || is_holiday) {
print("Day off!");
}
String Operations
+
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.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
Null Coalescing
??
Null Coalescing Operator
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";
}
// 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"
Range Operator
..
Range Operator
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
}