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 compound division assignment operator in Kotlin. It divides the left-hand operand by the right-hand operand and assigns the result to the left-hand operand, either by mutating the existing object’s internal state or by reassigning the variable reference.
a /= b

Operator Resolution Mechanics

Kotlin resolves the /= operator through its operator overloading conventions. It does not simply prioritize one function over another; instead, it evaluates the availability of both the divAssign and div functions. When the compiler encounters a /= b, it executes the following resolution sequence:
  1. Ambiguity Check: The compiler checks if both divAssign and div are applicable. If a.divAssign(b) is valid and a = a.div(b) is valid (meaning a is a var and the return type of div is assignable to a), the compiler reports a compile-time ambiguity error.
  2. divAssign Translation: If only the divAssign function is applicable, the operation is translated directly to this mutating function call. The function must return Unit.
  3. div Translation: If divAssign is not available, but the standard division operator function div is applicable and a is a var, the compiler translates the expression to evaluate a.div(b) and reassigns the resulting value back to a.
// Translation Path 1: Direct mutation via divAssign
a.divAssign(b) 

// Translation Path 2: Reassignment via div
a = a.div(b)

Technical Constraints

  • Mutability and References: The requirement for the left operand to be a mutable variable (var) depends strictly on the resolution path. If the operation resolves to divAssign, the left operand can be a read-only reference (val) because the function mutates the object’s internal state without reassigning the variable reference itself. A var is only required if the compiler utilizes the div reassignment fallback (a = a.div(b)).
  • Type Compatibility: When the compiler utilizes the div fallback, the return type of the div function must be a subtype of the declared type of the left operand. For example, dividing an Int by a Double yields a Double; attempting to implicitly reassign this back to an Int variable will fail at compile time.
  • Integer Truncation: If both operands are of standard integer types (e.g., Byte, Short, Int, Long), the underlying division operation performs integer division. Any fractional remainder is truncated towards zero before the assignment or mutation occurs.
  • Zero Division: If the right operand evaluates to 0, the operator behaves according to the underlying type’s division implementation. For integers, this throws an ArithmeticException. For floating-point types, it adheres to IEEE 754 standards, resulting in Infinity or NaN.
Master Kotlin with Deep Grasping Methodology!Learn More