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

# Kotlin Remainder Assignment

The `%=` operator is the augmented assignment operator for the remainder operation. It evaluates the remainder of the division of the left-hand operand by the right-hand operand and assigns that computed value back to the left-hand operand.

## Syntax and Desugaring

In Kotlin, `a %= b` is not a primitive language construct but is syntactically translated by the compiler into a corresponding function call. The resolution mechanism depends on the mutability of the left-hand operand and the available operator functions.

During compilation, the Kotlin compiler resolves this operator using the following rules:

1. **`remAssign` Resolution:** The compiler first looks for an `operator` member or extension function named `remAssign` on the left-hand operand's type. If found, `a %= b` is desugared to an in-place mutation:

```kotlin theme={"dark"}
a.remAssign(b)
```

Because this is an in-place mutation, the left-hand operand `a` can be a read-only reference (`val`), provided the object itself is mutable and the function modifies its internal state.
2\.  **`rem` Fallback:** If `remAssign` is not defined, the compiler falls back to the standard remainder operator function `rem`. In this scenario, `a %= b` is desugared to a reassignment:

```kotlin theme={"dark"}
a = a.rem(b)
```

For this fallback to compile, the left-hand operand `a` is strictly required to be a mutable reference (`var`), and the return type of `rem` must be assignable to the declared type of `a`.
3\.  **Ambiguity Error:** If *both* `remAssign` and `rem` are applicable (i.e., both functions are defined and the left-hand operand is a `var`), the compiler does not silently prioritize `remAssign`. Instead, it throws an assignment operator ambiguity error because it cannot safely determine whether to mutate the existing object or reassign the variable to a new object.

## Technical Characteristics

* **Remainder vs. Modulo:** Kotlin explicitly defines this as a *remainder* operation (`rem`), replacing the deprecated `mod` function from earlier versions. The sign of the result strictly follows the sign of the dividend (the left-hand operand). For example, `-5 %= 3` results in `-2`, whereas a true mathematical Euclidean modulo would result in `1`.
* **Return Type:** The `%=` operation is a statement, not an expression. When implemented via `remAssign`, the function must return `Unit`. Consequently, the operator cannot be chained or assigned to another variable (e.g., `val c = (a %= b)` is a compilation error).
* **Type Constraints:** When the compiler utilizes the `a = a.rem(b)` fallback, if `a.rem(b)` returns a wider type (e.g., `a` is `Int`, `b` is `Long`), the compiler will throw a type mismatch error.

## Operator Overloading

To enable the `%=` operator for custom types, you must explicitly overload the `remAssign` function using the `operator` keyword.

```kotlin theme={"dark"}
class Scalar(var value: Int) {
    // Overloading the %= operator
    operator fun remAssign(divisor: Int) {
        this.value = this.value.rem(divisor)
    }
}

// A read-only reference (val) is permitted because remAssign mutates internal state
val scalar = Scalar(14)
scalar %= 4 // Desugars to scalar.remAssign(4)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
