Operators
Autolang provides a rich set of operators for arithmetic, evaluation, collection manipulation, and capability orchestration.
Operator Reference
| Operator | Category | Description |
|---|---|---|
| + - * / % | Arithmetic | Standard arithmetic and modulus. Modulo or integer division by zero throws an exception. |
| ++ -- | Increment/Decrement | Increments or decrements integers or characters in place. |
| += -= *= /= %= | Compound Assignment | Updates variables, class fields, array indices, or map entries in place. |
| == != < <= > >= | Comparison | Value comparison across numbers, strings, characters, and types implementing compareTo. |
| === !== | Identity | Verifies reference/pointer equality in memory. |
| && || ! | Logical | Short-circuit logical AND, logical OR, and logical negation. |
| in !in | Membership | Checks existence within ranges, arrays, sets, map keys, or objects with contains. |
| .. ..< | Range | 1..10 creates an inclusive range; 1..<10 creates an exclusive upper-bound range. |
| ?: ?? | Elvis / Coalescing | Evaluates fallback value or triggers control transfer (return, break, throw) when null. |
| to | Pair Constructor | Infix operator creating a Pair(first, second). Used with maps. |
| is !is | Type Inspection | Checks whether a value is or is not an instance of the target type. |
| as | Type Cast | Explicitly casts an instance to a targeted type. |
Ranges and Membership Checks
Ranges integrate seamlessly with the in and !in operators, as well as for loops and when expressions.
val statusCode = 204
// Inclusive range: 200..299
val isSuccess = statusCode in 200..299
println("Status ok: " + isSuccess) // true
// Exclusive upper bound: 0..<5 (0, 1, 2, 3, 4)
val index = 5
val inRange = index in 0..<5
println("Index valid: " + inRange) // false
// Membership in collection
val validCurrencies = ["USD", "EUR", "GBP"]
if ("JPY" !in validCurrencies) {
println("Currency not supported")
}Collection Operators
Autolang supports expressive collection operators for concatenation and element subtraction without boilerplate loops.
// Array concatenation
val baseItems = ["SKU-01", "SKU-02"]
val combined = baseItems + ["SKU-03"] + ["SKU-04", "SKU-05"]
println("Total items: " + combined.size()) // 5
// Set union and difference
val setA = setOf(1, 2, 3)
val setB = setOf(3, 4)
val unionSet = setA + setB // {1, 2, 3, 4}
val diffSet = setA - 2 // {1, 3}
// Map merge and key removal
val configA = mapOf("timeout" to 30, "retries" to 3)
val configB = mapOf("retries" to 5, "debug" to 1)
val merged = configA + configB // "retries" updated to 5
val cleaned = merged - "debug"Compound Assignment on Complex Targets
Compound assignment operators (+=, -=, etc.) operate directly on nested object properties, array indices, and map values without multiple index Lookups.
val inventory = mapOf("laptops" to 20)
inventory["laptops"]! += 5
println("Inventory: " + inventory["laptops"]) // 25
val counters = [100, 200, 300]
counters[1] += 50
println("Second counter: " + counters[1]) // 250Operator Overloading (operator fun)
Autolang allows custom classes to overload standard language operators using the operator fun modifier (or the @operator annotation). When an operator expression is evaluated, the runtime automatically translates the syntax to the corresponding operator method.
| Expression | Function Name | Signature Pattern | Description |
|---|---|---|---|
| a + b | plus | operator fun plus(other: T): R | Binary addition or concatenation |
| a - b | minus | operator fun minus(other: T): R | Binary subtraction or difference |
| a * b | times | operator fun times(other: T): R | Binary multiplication or scaling |
| a / b | div | operator fun div(other: T): R | Binary division |
| a % b | rem | operator fun rem(other: T): R | Modulo / remainder |
| +a | unaryPlus | operator fun unaryPlus(): R | Unary positive |
| -a | unaryMinus | operator fun unaryMinus(): R | Unary negation (sign inversion) |
| !a | not | operator fun not(): Bool | Logical negation |
| <, <=, >, >= | compareTo | operator fun compareTo(other: T): Int | Ordering comparison (returns negative, 0, or positive) |
| a[i] | get | operator fun get(index: K): V | Indexed element access |
| a[i] = v | set | operator fun set(index: K, value: V) | Indexed element assignment |
| a in b, a !in b | contains | operator fun contains(element: T): Bool | Membership test in collections or bounds |
Arithmetic and Comparison Overloading
Domain models such as monetary amounts or metrics can encapsulate arithmetic rules, validation, and relative ordering.
class MonetaryAmount(val cents: Int, val currency: String) {
operator fun plus(other: MonetaryAmount): MonetaryAmount {
return MonetaryAmount(this.cents + other.cents, this.currency)
}
operator fun minus(other: MonetaryAmount): MonetaryAmount {
return MonetaryAmount(this.cents - other.cents, this.currency)
}
operator fun times(multiplier: Int): MonetaryAmount {
return MonetaryAmount(this.cents * multiplier, this.currency)
}
operator fun unaryMinus(): MonetaryAmount {
return MonetaryAmount(-this.cents, this.currency)
}
operator fun compareTo(other: MonetaryAmount): Int {
return this.cents - other.cents
}
}
val orderItem1 = MonetaryAmount(2500, "USD")
val orderItem2 = MonetaryAmount(1200, "USD")
val subtotal = orderItem1 + orderItem2
val scaled = orderItem1 * 2
val discounted = -orderItem2
println("Subtotal cents: " + subtotal.cents) // 3700
println("Scaled cents: " + scaled.cents) // 5000
println("Item 1 is greater: " + (orderItem1 > orderItem2)) // trueIndex Access and Membership Checking
Overloading get, set, and contains provides clean subscript indexing and collection membership semantics for custom collections or rate limit registries.
class RateLimitBucket(val limits: Array<Int>) {
operator fun get(tier: Int): Int = limits[tier]
operator fun set(tier: Int, limit: Int) {
limits[tier] = limit
}
operator fun contains(tier: Int): Bool = tier >= 0 && tier < limits.size()
}
val bucket = RateLimitBucket([100, 500, 2000])
println("Tier 1 limit: " + bucket[1]) // 500
bucket[1] = 750
println("Updated tier 1 limit: " + bucket[1]) // 750
println("Supports tier 2: " + (2 in bucket)) // true
println("Supports tier 5: " + (5 in bucket)) // false