AutolangDocs

Operators

Autolang provides a rich set of operators for arithmetic, evaluation, collection manipulation, and capability orchestration.

Operator Reference

OperatorCategoryDescription
+ - * / %ArithmeticStandard arithmetic and modulus. Modulo or integer division by zero throws an exception.
++ --Increment/DecrementIncrements or decrements integers or characters in place.
+= -= *= /= %=Compound AssignmentUpdates variables, class fields, array indices, or map entries in place.
== != < <= > >=ComparisonValue comparison across numbers, strings, characters, and types implementing compareTo.
=== !==IdentityVerifies reference/pointer equality in memory.
&& || !LogicalShort-circuit logical AND, logical OR, and logical negation.
in !inMembershipChecks existence within ranges, arrays, sets, map keys, or objects with contains.
.. ..<Range1..10 creates an inclusive range; 1..<10 creates an exclusive upper-bound range.
?: ??Elvis / CoalescingEvaluates fallback value or triggers control transfer (return, break, throw) when null.
toPair ConstructorInfix operator creating a Pair(first, second). Used with maps.
is !isType InspectionChecks whether a value is or is not an instance of the target type.
asType CastExplicitly 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]) // 250

Operator 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.

ExpressionFunction NameSignature PatternDescription
a + bplusoperator fun plus(other: T): RBinary addition or concatenation
a - bminusoperator fun minus(other: T): RBinary subtraction or difference
a * btimesoperator fun times(other: T): RBinary multiplication or scaling
a / bdivoperator fun div(other: T): RBinary division
a % bremoperator fun rem(other: T): RModulo / remainder
+aunaryPlusoperator fun unaryPlus(): RUnary positive
-aunaryMinusoperator fun unaryMinus(): RUnary negation (sign inversion)
!anotoperator fun not(): BoolLogical negation
<, <=, >, >=compareTooperator fun compareTo(other: T): IntOrdering comparison (returns negative, 0, or positive)
a[i]getoperator fun get(index: K): VIndexed element access
a[i] = vsetoperator fun set(index: K, value: V)Indexed element assignment
a in b, a !in bcontainsoperator fun contains(element: T): BoolMembership 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)) // true

Index 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