Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The *= operator is the augmented assignment operator for multiplication in Kotlin. It evaluates the product of the left-hand operand and the right-hand operand, subsequently applying the result to the left-hand operand either by mutating its internal state in place or by reassigning its variable reference.
a *= b

Compiler Resolution and Operator Overloading

Kotlin resolves the *= operator through a deterministic compiler translation mechanism. When the compiler encounters a *= b, it attempts to resolve the operation in the following order:
  1. timesAssign Resolution: The compiler first checks if the type of a defines a member or extension function named timesAssign that accepts the type of b. If found, the operation is executed in place, and the expression is translated to:
a.timesAssign(b)

   When resolved this way, `a` can be declared as a read-only variable (`val`). This is because the operation mutates the object's internal state in place without reassigning the variable reference itself.
2. **`times` Fallback:** If `timesAssign` is not defined, the compiler falls back to the standard multiplication operator function `times`. The expression is translated to:
   ```kotlin
a = a.times(b)
For this fallback to compile, a must be declared as a mutable variable (var). This is required because the variable reference itself must be reassigned to an entirely new value. Standard primitive types rely on this fallback because primitive values do not possess mutable internal state; their references must be reassigned. If both timesAssign and times are applicable and resolve successfully, the Kotlin compiler will throw an ambiguity error.

Custom Implementation Constraints

To explicitly define the *= behavior for a custom type without reassigning the object reference, you implement the timesAssign operator function. A critical constraint in Kotlin is that the timesAssign operator function must explicitly or implicitly return Unit. Unlike languages such as C++ or Java where assignment operators often return a reference to allow chaining (e.g., a *= b *= c), Kotlin strictly requires assignment operator functions to return Unit. Returning any other type results in a compilation error.
class Vector(var magnitude: Int) {
    operator fun timesAssign(scalar: Int): Unit {
        this.magnitude = this.magnitude * scalar
    }
}

Type Safety and Conversions

Unlike Java, Kotlin does not perform implicit narrowing conversions during augmented assignment. When relying on the times fallback (a = a * b), the type returned by the right-hand side evaluation must strictly match or be a subtype of the left-hand variable.
var a: Int = 5
val b: Double = 2.0

// Compilation Error: Type mismatch. 
// Required: Int. Found: Double.
a *= b 
In the example above, Int.times(Double) yields a Double. Because Kotlin requires explicit conversions, the Double result cannot be implicitly reassigned to the Int variable a.
Master Kotlin with Deep Grasping Methodology!Learn More