TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
-= operator is an augmented assignment operator that subtracts the right-hand operand from the left-hand operand and assigns the result back to the left-hand operand. In Kotlin, its behavior is resolved at compile-time based on the mutability of the left-hand operand and the presence of specific operator functions (minusAssign or minus).
Compiler Resolution
When the Kotlin compiler encountersa -= b, it attempts to resolve the operation in the following order:
minusAssignFunction: If the left-hand operand has an accessibleminusAssignmember or extension function that accepts the right-hand operand, the compiler translates the expression to a mutating operation. The return type ofminusAssignmust beUnit.minusFunction: IfminusAssignis not available, the compiler falls back to the standardminusoperator function. It evaluatesa.minus(b)and reassigns the result toa. This requiresato be declared as a mutable variable (var).
minusAssign and minus are applicable and a is a var, the compiler will throw an overload ambiguity error.
Primitive Types
For built-in primitive types (e.g.,Int, Double, Float), -= performs standard arithmetic subtraction and reassignment. The left-hand operand must be a var.
Collection Behavior
The-= operator exhibits distinct behaviors depending on whether it is applied to a read-only collection or a mutable collection.
Mutable Collections (MutableList, MutableSet, etc.)
The operator resolves to minusAssign. It mutates the existing collection in place by removing the specified element. Because the collection instance itself is modified, the reference can be declared as a val.
List, Set, etc.)
The operator resolves to minus. It creates a completely new collection containing all elements of the original collection except the subtracted element, and then reassigns the new collection to the variable. The reference must be declared as a var.
Operator Overloading
To enable the-= operator for custom classes, you must explicitly define the minusAssign operator function using the operator modifier.
Master Kotlin with Deep Grasping Methodology!Learn More





